编程语言
首页 > 编程语言> > javascript – 调用document.write()

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>

动态脚本添加了document.write(someCode,用于加载横幅).但在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
来源: https://codeday.me/bug/20190521/1147708.html