Javascript 如何延迟加载jQuery本身,特别是在使用$(document).ready()时
我在我的网站上使用jQuery,如下所示:Javascript 如何延迟加载jQuery本身,特别是在使用$(document).ready()时,javascript,jquery,lazy-loading,document-ready,Javascript,Jquery,Lazy Loading,Document Ready,我在我的网站上使用jQuery,如下所示: <head> <script src="http://ajax.googleapis.com/.../jquery.min.js" ...></script> </head> 在某些情况下,此事件用于: $(document).ready(function(){ var s = document.createElement("script"); s.type = "text/javasc
<head>
<script src="http://ajax.googleapis.com/.../jquery.min.js" ...></script>
</head>
在某些情况下,此事件用于:
$(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事件失败。最好的解决方法是什么?您可以在页面底部加载jQuery,而不是在
中加载。它仍将使用带宽,但在视觉上应该更快 也许这篇最近的文章可以帮助您:
后面的想法是创建一个临时的<代码> $< /代码>函数,在其中收集所有要在<代码> doMeDe> <代码>事件中执行的函数,然后在页面底部加载jQuery时再替换它。
这意味着我不能在页面中间使用DojtRe.Read事件(不确定是否阻止浏览器)。$(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);
});