Jquery - getScript 版本<脚本>..

发布于 2024-10-21 09:53:05 字数 240 浏览 6 评论 0原文

什么更快?

<script src="../js/SOME.js" type="text/javascript"></script>

或者

   $.getScript('../js/SOME.js', function (){ ... 
   // with $.ajaxSetup({ cache: true }); 

what is faster?

<script src="../js/SOME.js" type="text/javascript"></script>

OR

   $.getScript('../js/SOME.js', function (){ ... 
   // with $.ajaxSetup({ cache: true }); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

椒妓 2024-10-28 09:53:05

我猜 更快,因为浏览器本身就是这样做的,而第二种选择首先强制浏览器加载页面,然后使用 JavaScript 加载脚本。

浏览器可能会自行处理缓存,但我不太确定。

I'd guess that <script src="../js/SOME.js" type="text/javascript"></script> is faster, as the browser does it natively, while the second alternative first forces the browser to load the page, then use JavaScript to load the script.

The browser might take care of caching by itself, but I'm not too certain.

小姐丶请自重 2024-10-28 09:53:05

他们是一样的。但这是您应该考虑的事实:

  • 要使用 getScript,您需要有
    首先加载 jQuery,所以添加那个时间(我猜这就是你正在使用的,因为 $)。

  • jQuery 会异步加载它,这意味着
    浏览器不会停止其他一切
    加载 SOME.js。

They are the same. But this are facts you should take into account:

  • To use getScript you need to have
    loaded jQuery first so add that time (i'm guessing that is what you are using becuase of the $).

  • jQuery would load it asynchronously which means the
    browser won't stop everything else to
    load SOME.js.

柳若烟 2024-10-28 09:53:05

前者,因为 $.getScript 依赖 jquery 来初始化。

The former, since $.getScript relies on jquery to be initialized.

倦话 2024-10-28 09:53:05

它们的下载时间大约相同。不同之处在于,内联脚本与页面上的所有其余元素一起加载,因此必须竞争带宽。

注入脚本将在页面加载后和 jQuery 加载后进行。由于此时可能已下载其余页面元素,因此它看起来“更快”,但“稍后”即可使用。

They will both take approximately the same time to download. The difference is that the inline script loads with all the rest of the elements on the page, and therefore must compete for bandwidth.

Injecting the script will take place after the page had loaded and after jQuery has loaded. Since the rest of the page elements are likely downloaded by this time it will seem "faster" but will be ready to use "later".

天涯离梦残月幽梦 2024-10-28 09:53:05

最快的方法是使用如下脚本同步加载脚本:

<script id="your-script-id" type="text/javascript">

(function() {
 var your-script-id = document.createElement('script');
 your-script-id.type = 'text/javascript';
 your-script-id.src = ('http://your-script-location.js');
 var s = document.getElementById('your-script-id');
 s.parentNode.insertBefore(your-script-id, s);
})();

</script>

The fastest would be to load the scripts synchronously whith a script like:

<script id="your-script-id" type="text/javascript">

(function() {
 var your-script-id = document.createElement('script');
 your-script-id.type = 'text/javascript';
 your-script-id.src = ('http://your-script-location.js');
 var s = document.getElementById('your-script-id');
 s.parentNode.insertBefore(your-script-id, s);
})();

</script>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文