Dynamic Script Adding a tag asynchronously?

Is dynamic Script tag addition asynchronous? How dynamic, including a collection of JavaScript files from another domain.

+4
source share
2 answers

Yes, it is asynchronous. Dynamic <script> injection always causes the browser to load an external resource through the DOM (for example, as style sheets, images, flash), which must be executed asynchronously to avoid blocking the browser.

Can you randomly take a look at JSONP ("JSON with Padding")? It uses dynamic script tag injection. There are more and more discussions about “AJAX,” and the fact that it is not possible to execute synchronous JSONP (for example, synchronous XmlHttpRequest ) is often skipped.

+6
source

Is dynamic Script tag addition asynchronous?

Yes. It is considered asynchronous mainly because of what happens when the Script tag is not dynamically inserted.

A fully-formed Script tag that exists in the DOM will lead to blocking behavior, that is, the page will not be able to continue loading other HTML until this particular asset finishes loading. Thus, many developers prefer to place Script tags immediately before the </body> . So the tag is called synchronous .

In contrast, if you dynamically insert a Script tag, it does not show blocking. I say because, as you saw in other comments, not all browsers do this. It is best to wrap this dynamic Script code in some document ready callback. Thus, on the contrary, it is technically asynchronous , because other assets can continue to function along with this asset and its functionality.

0
source

All Articles