我知道 document.ready() 但我不想等待外部服务器上的某些内容(例如 Google Analytics)、某些广告服务内容或网站并非绝对需要的任何其他内容。我在有用户评论的网站上看到了这个问题,通常每个外部内容都必须在评论可用之前加载,即使许多 CDN 中的一个迟到了,它也会阻止其他所有内容。
问问题
49 次
1 回答
1
I don't think you need to test it as much as you need to defer it. Here is a really simple example from Google.
(the following code copied from Google)
// Add a script element as a child of the body
function downloadJSAtOnload() {
var element = document.createElement("script");
element.src = "deferredfunctions.js";
document.body.appendChild(element);
}
// Check for browser support of event handling capability
if (window.addEventListener)
window.addEventListener("load", downloadJSAtOnload, false);
else if (window.attachEvent)
window.attachEvent("onload", downloadJSAtOnload);
else window.onload = downloadJSAtOnload;
Essentially, add a new script tag at the documentReady state when you're executing your code already.
于 2012-08-20T03:28:27.953 回答