计算机技术

异步加载js不能使用document.write

document已经加载解析完毕,文档流已经关闭了

所以你异步加载的js不可以再往document里写东西了,比如使用document.write

不过你可以用dom方法添加

.appendChild()

.insertBefore()

.innerHTML

解决方法:你可以把script加载放在body标签结束之前

例如:

复制代码
<script>
var _hmt = _hmt || [];
(function() {
var hm = document.createElement("script");
hm.src = "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js";
var s = document.getElementsByTagName("script")[0];
s.parentNode.insertBefore(hm, s);
})();
</script>
复制代码

 

Related Articles

发表回复

Check Also
Close
Back to top button