I've added the async
attrib to my javascript inclusion HTML.
So now I've:
<script async src="jquery.myscript.js"></script>
And this works with all JS I load, all except jquery.
If I add async
to jQuery <script>
tag all others script who depend from jquery don't work.
In that jsfiddle you can see the problem:
http://jsfiddle.net/uFbdV/
In the example I've used <script> Mycode </script>
instead of including an external file.js, but this doesn't change the situation.
I'd like to run jQuery with async attrib and run other few external scripts asynchronously only after jquery is loaded.
It is possible?
This may be what you are looking for:
http://www.yterium.net/jQl-an-asynchronous-jQuery-Loader - jQl an asynchronous jQuery Loader.
It asynchronously loads jQuery without blocking other components:
jQl automatically catches all
jQuery().ready()
calls, so you can write:jQl will queue these function calls, and execute them as soon as jQuery is loaded and the DOM is ready, as they would be executed in the usual way.
This is a great use-case for
defer
:Neither of these tags will block rendering. The browser can download
jquery.js
andcustom.js
in parallel. It will executejquery.js
as soon as it has been downloaded and the DOM has been loaded, andcustom.js
will execute afterwards.Sadly, there a few issues:
defer
attribute for inline scripts.defer
to unexpectedly interleave their execution.defer
order are meant to be executed in order, just that they are meant to be executed after the DOM loads.Using a recursive function
I have written a function to do this job very nice.
You can put these code in a file like
scripts.js
and load it withasync
attribute.For css async load, refer to edited versions of this answer.
fixed it
What does that mean? It sounds a lot like you want to load jQuery first, then other things when it's done. So you want to load it synchronously. If you still want to use the
async
way, you could define anonload
function to continue loading other things once jQuery is ready. Or you could usedefer
. Both of these are explained here: https://davidwalsh.name/html5-async