【javascript】如何延迟加载JavaScript(Google推荐的代码)

下面是Google推荐的代码。这些代码应被放置在</body>标签前(接近HTML文件底部)
<script type="text/javascript">
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "defer.js";
document.body.appendChild(element);
}
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
</script>

posted @ 2016-04-21 16:19  小泥巴2008  阅读(266)  评论(0编辑  收藏  举报