Dynamic Script Tag Addition is Asynchronous?

夙愿已清 提交于 2019-12-23 10:26:33

问题


Is Dynamic Script Tag Addition is Asynchronous? Like dynamically including set of JavaScript files from a different domain..


回答1:


Yes, it is asynchronous. Dynamic <script> injection always results in the browser loading an external resource via the DOM (e.g. just like stylesheets, images, flash), which must happen asynchronously to avoid browser lockup.

Are you looking at JSONP ("JSON with Padding") by any chance? It uses dynamic script tag injection. It's more and more part of discussions about "AJAX", and the fact that it is impossible to do synchronous JSONP (like synchronous XmlHttpRequest) is often overlooked.




回答2:


Is Dynamic Script Tag Addition is Asynchronous?

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

A fully formed script tag that exists in the DOM will cause blocking behavior, i.e. the page cannot continue loading other HTML until that particular asset is finished loading. Thus, many developers prefer to place their script tags just before the </body> tag. In that way, a tag becomes referred to as synchronous.

By contrast, if you dynamically inject a script tag, it tends to not exhibit blocking behavior. I say tends because as you've seen in other comments, not all browsers do that. The best thing is to wrap that dynamic script code in a document ready callback of some kind. Thus, by contrast, it is technically asynchronous because other assets can continue to function alongside that asset and its functionality.



来源:https://stackoverflow.com/questions/1491677/dynamic-script-tag-addition-is-asynchronous

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!