Question

I am just wondering when <script src="http://...example.js"/> is encountered in an HTML file, what will happen? Is there some event handler or there will be something like a get request sent?

Était-ce utile?

La solution 3

This is described in HTML5 CR, in the section on the script element. In the example case, since neither the defer nor the async attribute is used, the browser will fetch (with a GET request) and execute the script before continuing the processing of the HTML document. The default handling is thus synchronous blocking behavior.

However, if you really use markup like <script src="http://...example.js"/> without an end tag </script>, the script is not fetched at all, except in the rare case where you serve the HTML document with an XML content type. The reason is that <script src="http://...example.js"/> is parsed as a start tag only, so all the rest is parsed as content of a script element, and when no end tag is parsed, the script element is not completed. So don’t try to use “self-closing” syntax; instead, write <script src="http://...example.js"></script>.

Autres conseils

In general, the browser stops everything it's doing--stops rendering, stops the event loop, stops listening to or acting on input. This state is called "blocking." It then executes whatever is in the script block, or retrieves the script which is referenced in the src attribute via a GET request, then executes it when it arrives. After execution, the browser comes out of blocking and continues to parse the DOM.

If I remember correctly, script sources aren't loaded asynchronously, which means execution will stop until it's loaded. As such, there's no need for an event that announces when it's done. Hopefully someone will correct me on this if I'm wrong.

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top