2012-03-15 37 views
3

我使用jQuery在我的網站是這樣的:

<head> 
<script src="http://ajax.googleapis.com/.../jquery.min.js" ...></script> 
</head> 

然後我用:

$(document).ready(function(){ 
}); 

在某些情況下,使用此事件:

$(document).ready(function(){ 
    var s = document.createElement("script"); 
    s.type = "text/javascript"; 
    s.async = true; 
    s.src = "http://www.script-host.com/.../script.js"; 
    document.getElementsByTagName("head")[0].appendChild(s); 
}); 

現在的jquery.js似乎是(一)在文件大小方面對我的網站上最重的資源。因此,我想延遲加載jquery.js本身,但我知道這會導致所有document.ready事件失敗。什麼是最好的解決方法?

回答

3

您可以在頁面底部加載jQuery,而不是在<head>。它仍然會使用帶寬,但它應該在視覺上更快。

+1

這將意味着我不能在頁面中間的某處使用document.ready事件(不知道是否阻止瀏覽器)。 – 2012-03-15 08:50:59