A
A
Alexander Yudaev2016-01-08 20:13:59
JavaScript
Alexander Yudaev, 2016-01-08 20:13:59

What options for asynchronous loading can be used with inline js code?

Good day,
I have a specific question, I have been optimizing the speed of sites for a long time, but I could not cope with one problem, the situation is very standard, a WordPress template and a few plugins (at the moment there is a problem with the Revolution slider).
jQuery is loaded asynchronously (async / defer) from the server itself or cdn (not so important),
code example

<script defer src="//code.jquery.com/jquery-1.11.2.min.js"></script>
<script>
$( document ).ready(function() {
    console.log( "ready!" );
});
</script>

The situation is this, HTML seems to finish loading and jQuery is not connected yet, well, a standard error. jQuery is not defined and so on.
I tried a lot of ways, to drive the code from the script without href into arrays and then restart, even manually rewrite the code and load it, something like that
function jQueryWaiting() {
    if (typeof jQuery === "undefined") {
         setTimeout(jQueryWaiting,100);
        return;
    }
 
    $(document).ready(function(){
        console.log( "ready!" );
    });
}
jQueryWaiting();

in principle, everything works, but I don’t want to rewrite every script or plugin.
Is there some other way?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
R
Roman Makarov, 2016-01-09
@rmakarov

If you are concerned about speed and optimization, then shouldn't all scripts be concatenated in the order you need, minified, and then just load 1 file? The code from any tutorial about gulp\grunt will solve this problem with a bang.
Or wrap the initialization of the slider in(function ($) { //init slider here})(jQuery);

S
Sergey, 2016-01-08
Protko @Fesor

we take any loader of scripts. Inline it in the body of the page. We load them with jquery and other things.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question