He wrote in the English version of the site, after which he got here, who did not understand the question, sorry.

I create a Javascript structure resembling a Sandbox Core and modules (in my case with the IIFE structure). I like the idea of ​​connecting scripts inside scripts (like the analogy include or required). I am connecting the script via XMLHttpRequest and eval:

var transport = js.getXHTTPTransport(); // my function transport.open('GET', js.pathToUrl(path, version), false); transport.send(null); 

I see downloaded scripts, you can see them in the Network tab of the debugger, but there is a warning:

Synchronous XMLHttpRequest on the main thread is deprecated

Can you advise:

  1. Is this a normal way to connect scripts in this way, so to speak, taking into account modern frameworks and plugins (I don’t want to use a separate library like RequireJS) just for the connection function? And will it affect the page loading speed? There are many examples, but it’s not clear to me whether this is a good / normal practice of such a "shadow" connection and whether it affects the page. For me, this is the convenience of connecting the necessary libraries, precisely within a specific file: https://www.safaribooksonline.com/library/view/even-faster-web/9780596803773/ch04.html
  2. Is it possible to get rid of warning and is it significant? All the same, a warning is a warning, not really Camille, or is it better to connect scripts by the same script tag injection on the page? Last, I somehow do not really like it.

Thanks for the help.

  • Translated as you requested. If not zaturdnit, advise someone useful something. Thank. - Kirill Malyarenko
  • a warning simply says that an asynchronous request should be used, more specifically, to replace false in the open method with true - Grundy
  • @Grundy Putting this and connecting say, jquery ( js.include('libraries/jquery/jquery-1.11.3.min'); ), the library does not work asynchronously. I understand that everything happens at different times and something is loaded before the other. With a value of false everything happens "by script", but causes a warning. Is setTimeout correct in this case? - Kirill Malyarenko 6:55
  • correctly, you can just load the next module by the dependency loading event - Grundy
  • why setTimeout? - Grundy

1 answer 1

As such, no one gave an answer, and as it turned out, this is a specific question, depending on your need and desire. Immediately I want to recommend the book: Even Faster Websites .

It has interesting statistics and postscript: Using eval, potentially slower than using Injection - dynamic creation of a DOM script (Using eval is potentially slower than using this mechanism). However, there is still a need to find out the answer from the ready object, which doesn’t work everywhere in the textbook.

After that, personally for myself, I decided to stop at a similar creation of scripts and which, judging by statistics, is much faster than the analogs:

 var scriptElement = document.createElement('script'); scriptElement.src = 'js/' + moduleName + '.js'; scriptElement.setAttribute('data-module-name', moduleName); scriptElement.addEventListener("load", function(){ setTimeout(_resolve, 0); // функция добавления в фабрику модулей }); document.body.appendChild(scriptElement); 

Thus, you can connect the necessary dependencies in the desired file:

 define('core', ['libraries/jquery/jquery-1.11.3.min'], function(jQuery) { 

creating some sort of work RequireJS. To whom further study is interesting, one can turn to this solution (I don’t know why some methods of use were suggested and as stated at the beginning of the article this is a purely theoretical approach, which turned out to be very practical): https://github.com/sunpig/requirejs-from-scratch

Or another example of a library that I did not use, but with a similar goal: www.getcodesamples.com/src/46553AE5/F5352C78

I hope it will be useful to someone and to some extent answers the question.