为什么document.write不能使用动态加载的脚本?

时间:2014-01-13 10:08:52

标签: javascript dom asynchronous

我对这种行为有点困惑。 情况是:我们有一个渲染文件。 The specifiaction说,如果我们在呈现文档时运行此方法,它将清除当前文档。我在控制台中运行它:

document.write("Hi!")

并使用“嗨!”获取空文档文本。

但动态和异步加载的脚本中的相同代码不执行任何操作:

var s = document.createElement('script');
s.src = "some/path/test.js"
s.type = "text/javascript";
document.getElementsByTagName("head")[0].appendChild(s);

test.js包含:

document.write("Hi!")

我知道,我可以重新定义document.write等等。我只是询问规范 - 指定了这种行为?

谢谢!

1 个答案:

答案 0 :(得分:0)

刚刚在mdn找到了一些信息:

  

永远不要从异步脚本调用document.write()。在Gecko 1.9.2中,调用document.write()具有不可预测的效果。在Gecko 2.0中,从异步脚本调用document.write()无效(除了向错误控制台打印警告)。

相关问题