调用document.write() [英] Invoking 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中,我有一个错误:
The dynamic script adds document.write(someCode which loads banners)
. But in Firebug I have an error:
从异步加载的外部脚本中调用document.write()被忽略.
Invoking document.write() from asynchronously-loaded external script was ignored.
推荐答案
添加此内容:
newScript.async = false;
您的脚本需要同步加载才能使 document.write()
正常工作(请参见
Your script needs to load synchronously for document.write()
to work (see https://developer.mozilla.org/En/HTML/Element/Script#attr-async). As you have it now, the script will load whenever the browser has time for it - so you cannot know where your HTML from document.write()
will be inserted. The browser decided to ignore your document.write()
call to prevent worse issues.
这篇关于调用document.write()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!