Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it safe to use async with external js files?

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)?

like image 412
B. Clay Shannon-B. Crow Raven Avatar asked Jan 21 '26 16:01

B. Clay Shannon-B. Crow Raven


1 Answers

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.

like image 69
T.J. Crowder Avatar answered Jan 24 '26 08:01

T.J. Crowder