Is the script a reliable respite?

I currently serve all javascripts merged into one large file through Amazon Cloudfront. But since jQuery is so great, I am thinking about using the version provided by Google. Of course, I would include script tags at the bottom of the page and add the defer attribute if I hadn't read this article: http://hacks.mozilla.org/2009/06/defer/

If I understand correctly, the defer attribute only works correctly in Firefox, while every other browser (at that time) will execute scripts in random order. It's true? Of course, my scripts depend on jQuery, so it must be executed before my scripts.

+4
source share
3 answers

I'm not sure I will worry if you are not sure of his support. Just do it instead:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.4/jquery.js"></script> <script>window.jQuery || document.write("<script src='localJSFolder/jquery-1.6.4.min.js'></script>")</script> 

All modern browsers must run scripts sequentially. This is the easiest way to use Google CDN with local backup.

0
source

I would suggest something like this (using window.onload to really behave like defer):

 $(window).load(function () { var script = document.createElement("script"); script.type = "text/javascript"; if (script.readyState) { // IE script.onreadystatechange = function () { if (script.readyState === "loaded" || script.readyState === "complete") { script.onreadystatechange = null; // do something } }; } else { // Others script.onload = function() { // do something (the same thing as above) }; } script.src = file; document.getElementsByTagName("head")[0].appendChild(script); }); 

If you need more than one file, put in a loop and set the file to something like the file [x]

0
source

In 2017, it seems reliable enough to stop avoiding these 5 letters in favor of workarounds for 17-liners. Browser support for defer not bad these days

0
source

Source: https://habr.com/ru/post/1395208/


All Articles