Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/426.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何延迟加载jQuery本身,特别是在使用$(document).ready()时_Javascript_Jquery_Lazy Loading_Document Ready - Fatal编程技术网

Javascript 如何延迟加载jQuery本身,特别是在使用$(document).ready()时

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

我在我的网站上使用jQuery,如下所示:

<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);
});