调用document.write()

lev*_*von 5 javascript document.write

此代码不起作用:

<div class="pix"><div id="addTimestamp"></div></div>
<script type="text/javascript">
(function () {
    var date = new Date(), 
        timestamp = date.getTime(),
        newScript = document.createElement("script");

    newScript.type = 'text/javascript';
    newScript.src = 'someUrl=' + timestamp + '?';
    document.getElementById('addTimestamp').appendChild(newScript);
}())
</script>
Run Code Online (Sandbox Code Playgroud)

动态脚本添加document.write(someCode which loads banners).但在Firebug中我有一个错误:

从异步加载的外部脚本调用document.write()被忽略.

Wla*_*ant 4

添加这个:

newScript.async = false;
Run Code Online (Sandbox Code Playgroud)

您的脚本需要同步加载才能document.write()工作(请参阅https://developer.mozilla.org/En/HTML/Element/Script#attr-async)。正如您现在所拥有的,只要浏览器有时间,脚本就会加载 - 因此您无法知道 HTMLdocument.write()将插入到哪里。浏览器决定忽略您的document.write()调用以防止出现更严重的问题。