与外部js文件使用异步是否安全?(Is it safe to use async with external js files?)

编程入门 行业动态 更新时间:2024-10-26 08:34:08
与外部js文件使用异步是否安全?(Is it safe to use async with external js files?)

从我读过的内容中,在对外部js文件的引用中使用“async”,例如:

<script scr="http://someCDN/bla.js" async />

可能会提供最好的表现。 但是,这样引用的脚本似乎也存在潜在的问题。 但是对于jQuery和jQueryUI(或任何基于jQuery的js文件),你需要首先加载jQuery,然后才能加载jQueryUI(或fancybox,或goMap,或者不管是什么情况)。

那么:异步是一种挑逗,还是可以在这种情况下安全使用(引用jQuery及其在CDN上托管的产卵)?

From what I've read, using "async" in references to external js files, such as:

<script scr="http://someCDN/bla.js" async />

might provide the best performance. However, there also seems to be a potential issue with scripts thus referenced executing in any which order. But for jQuery and jQueryUI (or any jQuery-based js file), you need jQuery to load first, and only thereafter jQueryUI (or fancybox, or goMap, or whatever the case is).

So: Is async a tease, or can it be safely used in such situations (referencing jQuery and its spawn hosted on CDNs)?

最满意答案

您可以使用不相关的脚本安全地使用async 。 但是如果你需要按顺序加载它们(例如你的例子),你不能只是对它们进行async并希望最好 - 你将创造一个竞争条件。

我发现这篇关于加载脚本的各种方法的文章非常有用,FWIW。

You can use async safely with unrelated scripts. But if you need to load them in order (such as your example), you can't just put async on both of them and hope for the best — you'll be creating a race condition.

I found this article on various ways to load scripts quite useful, FWIW.

更多推荐

jQuery,js,async,引用,jQueryUI,电脑培训,计算机培训,IT培训"/> <meta name="d

本文发布于:2023-07-24 20:13:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1250598.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   safe   js   files   external

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!