Quantcast
Channel: jQuery Forum
Viewing all articles
Browse latest Browse all 18

Re : Why does _evalUrl() use async:false?

$
0
0
When you inject HTML into a document and it includes <script> tags, jQuery runs the script synchronously so that on the next statement after the injection that script will have loaded/run and can be used. Some old-school templating systems depend on this to build HTML-and-script templates. If the script were run asynchronously the code wouldn't know when it actually was safe to use the final template. 

I definitely think it's better to avoid injecting scripts via HTML, especially if the script is remote and the page will be blocked while it is fetched and executed. You can use `$.getScript()` for that and it happens asynchronously.

If you think a note is warranted it would be good to think about where that note might go and file an issue at https://github.com/jquery/api.jquery.com/issues . Note that HTML can be inserted by dozens of jQuery APIs, by design, and script tags can pretty much go anywhere any other HTML does.

Viewing all articles
Browse latest Browse all 18

Trending Articles