Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jquery-ui/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 尝试打开文件并写入时出现问题_Javascript_Dom - Fatal编程技术网

Javascript 尝试打开文件并写入时出现问题

Javascript 尝试打开文件并写入时出现问题,javascript,dom,Javascript,Dom,有人能解释一下为什么下面的简单脚本在Firefox中不起作用,我该如何修复它 <script type="text/javascript"> var w = window.open("a.php"); w.document.write("hello"); </script> var w=window.open(“a.php”); w、 写下“你好”; 非常感谢我很确定你不能用javascript读/写文件,因为它是客户端语言?我可能完全错了 编辑: 请尝试以下代码:

有人能解释一下为什么下面的简单脚本在Firefox中不起作用,我该如何修复它

<script type="text/javascript">
var w = window.open("a.php");
w.document.write("hello");
</script>

var w=window.open(“a.php”);
w、 写下“你好”;

非常感谢

我很确定你不能用javascript读/写文件,因为它是客户端语言?我可能完全错了

编辑:

请尝试以下代码:

var new_window, new_window_content = [];
new_window_content.push('<html><head><title>New Window</title></head>');
new_window_content.push('<body>New Window</body>');
new_window_content.push('</html>');
new_window = window.open("", "", "status,height=200,width=200");
new_window.document.write(new_window_content.join(''));
var新窗口,新窗口内容=[];
新窗口内容推送(“新窗口”);
新窗口内容推送(“新窗口”);
新窗口内容推送(“”);
新窗口=窗口。打开(“,”,“状态,高度=200,宽度=200”);
new_window.document.write(new_window_content.join(“”));

干杯,肖恩

向在这里发布答案的其他人说:他并不是试图打开一个文件并向其写入内容——在浏览器中用javascript是不可能的。相反,他试图用
w.document.write
来写他刚刚在浏览器中打开的网页的末尾

Google Chrome表示此代码失败原因如下:

>>> var w = window.open("http://www.google.co.uk");
undefined
>>> w.document.write("lol");
TypeError: Cannot call method 'write' of undefined
首先需要使用
document.open()
打开文档流-这将在
w.document
中添加
write
函数:

<script type="text/javascript">
var w = window.open("a.php");
w.document.open();
w.document.write("hello");
w.document.close();
</script>

var w=window.open(“a.php”);
w、 document.open();
w、 写下“你好”;
w、 document.close();
(编辑以使代码示例显示更好)

DOM脚本更为先进,也更为健壮

e、 g


嘿,你没读这里的问题吗?回答之前请仔细阅读问题。对不起-标题应该是“打开一个窗口并在其中写入内容”。一个编程论坛上的当前标题表明了另一种情况。你能在“不起作用”上加个题吗?你期望它做什么,它没有做什么?@peSHIropen和
write
不引用文件io。不要在OP没有添加的时候添加这些-这只会增加这里的混乱。哦,对不起!我将避免编辑我基本上一无所知的东西。实际上并没有通读所有内容:唯一的答案是关于文件io的讨论..:-/我否决了这一点,因为document.write()函数不是他应该在这里使用的函数。有关更好的方法,请参见下面Dawn的答案,即在新页面中向DOM元素添加“hello”。
var w = window.open("a.php");
w.onload = function(){//you could also use dom ready rather than onload but that is a longer script that I've not included here
  var body = w.document.getElementsByTagName("body")[0];
  body.appendChild(document.createTextNode("bar"));
}