document.writeln之后的alert或setTimeout

时间:2014-03-03 01:20:20

标签: javascript firefox

我在Firefox版本27.0.1上遇到此问题 当我使用

document.writeln('whatever');

我不能使用简单的setTimeout函数甚至是警报 我可以在上一段代码之前显示警告但不在之后显示警告 这有效:

alert('hello');
document.writeln('whatever');


这不起作用:

document.writeln('something');
alert('blah...');
在firebug控制台中它说:

  

NS_ERROR_XPC_SECURITY_MANAGER_VETO:


这也不起作用:

document.writeln('text');
setTimeout(function(){
    alert('quak');
    document.writeln('000');
}, 1000);

控制台中没有错误但没有任何反应,实际上 setTimeout() 中的代码不会在 document.writeln之前或之后运行() ...或 document.write() 但如果我评论或删除这些行,则它可以正常工作

我在Google Chrome中测试了这个没问题。

1 个答案:

答案 0 :(得分:2)

document.writeln (或 document.write )会覆盖所有文档的内容。因此,你将在下面写下的那一行变得无关紧要。

如果您想添加文字到现有文档,请使用

var text = document.createTextNode("text");
document.body.appendChild(text);