此代码无效:
<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>
动态脚本添加document.write(someCode which loads banners)
。但是在 Firebug 中我有一个错误:
Invoking document.write() from asynchronously-loaded external script was ignored.
最佳答案
添加这个:
newScript.async = false;
您的脚本需要同步加载才能使 document.write()
正常工作(参见 https://developer.mozilla.org/En/HTML/Element/Script#attr-async )。正如您现在拥有的那样,脚本将在浏览器有时间加载时加载 - 因此您无法知道 document.write()
中的 HTML 将被插入到哪里。浏览器决定忽略您的 document.write()
调用以防止出现更严重的问题。
关于javascript - 调用 document.write(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7255526/