加载jquery脚本的顺序 [英] The order in which load jquery scripts

查看:59
本文介绍了加载jquery脚本的顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在客户开发方面比较新,所以也许我的问题对某些人来说似乎太幼稚了. 例如,如果我按以下顺序加载jQuery库:

I am newer in client development so maybe my question will seem naive to some of you. For example,if I have this order of loading jQuery libraries:

<script type="text/javascript" src="/jquery.js"></script>
<script type="text/javascript" src="/jquery-ui.js"></script>
<script type="text/javascript" src="/jquery-ui.min.js"></script>

加载jQuery库的顺序如下:

And this order of loading jQuery libraries:

<script type="text/javascript" src="/jquery-ui.js"></script>
<script type="text/javascript" src="/jquery-ui.min.js"></script>
<script type="text/javascript" src="/jquery.js"></script>

是否一样? 加载jQuery脚本的顺序重要吗?

提前谢谢!

推荐答案

在您的示例中,脚本将按顺序加载和执行,这是必要的. jQuery UI要求jQuery在网页上可用.

In your example, the scripts will be loaded and executed in order, which is necessary. jQuery UI requires jQuery to be available on the webpage.

这也意味着该网页的其余部分将等待请求并执行这些脚本,然后再继续下一个要加载的内容.

This also means that the rest of the webpage will wait for these scripts to be requested and executed before moving on to the next thing to load.

脚本标签将同步加载.但是,您可以向它们添加async属性,以使其异步加载.也就是说,无法预测它们的加载和执行顺序. jQuery通常不应该异步加载,因为在使用jQuery时,通常会有依赖于它的代码.

Script tags in the head section of the page will load synchronously by default, as I described above. However, you can add the async attribute to them to make them load asynchronously; meaning, the order in which they are loaded and executed cannot be predicted. jQuery generally should not be loaded asynchronously, since, when using it, you generally have code that is dependent on it.

<script src="..." async></script>

以上代码将异步加载脚本,这意味着将随机加载脚本(您永远无法预测),并且在加载过程中不会锁定页面.在其他情况下这非常有用!

The above code will load the script asynchronously, meaning it will be loaded randomly (you can never predict it), and it will not lock up the page during loading. This is very useful in other cases!

这篇关于加载jquery脚本的顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆