<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://wiki.whatwg.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Getify</id>
	<title>WHATWG Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://wiki.whatwg.org/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Getify"/>
	<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/wiki/Special:Contributions/Getify"/>
	<updated>2026-04-03T18:14:40Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.3</generator>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=8483</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=8483"/>
		<updated>2012-09-12T15:17:12Z</updated>

		<summary type="html">&lt;p&gt;Getify: updating link to spec location for discussion about the &amp;quot;true preloading&amp;quot; technique.&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
4. Run-away memory usage (see [[Script_Execution_Control#Limitations_2|2.2.1.3]])&lt;br /&gt;
&lt;br /&gt;
5. Doesn&#039;t support inline script &amp;quot;preloading&amp;quot; (see [[Script_Execution_Control#Limitations_2|2.2.1.2]])&lt;br /&gt;
&lt;br /&gt;
6. This proposal doesn&#039;t specify fetch error handling.  onerror is currently not fired on scripts which are not in a document.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/the-script-element.html#script-processing-src-prepare HTML Spec, 4.3.1, &#039;Preparing a script&#039;]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 14 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
* a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
5. This proposal doesn&#039;t specify fetch error handling.  onerror is currently not fired on scripts which are not in a document.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 3 (Glenn Maynard) === &lt;br /&gt;
&lt;br /&gt;
This proposal keeps changes to script preparation and execution to a bare minimum.  Changes are described in terms of actual spec changes, to describe its behavior clearly and to show how it fits in with the existing spec.  Briefly, a &amp;quot;noexecute&amp;quot; attribute is added to block execution of the script, which is cleared to reenable execution.  More precisely:&lt;br /&gt;
&lt;br /&gt;
* Add the following attribute definition:&lt;br /&gt;
  The noexecute attribute is a boolean attribute that indicates that the&lt;br /&gt;
  script should not be executed.&lt;br /&gt;
* Add the following flag definition:&lt;br /&gt;
  The fifth is a flag indicating that a script is &amp;quot;pending-execution&amp;quot;.  Initially,&lt;br /&gt;
  script elements must have this flag unset.  This flag is used to trigger execution&lt;br /&gt;
  if execution was deferred due to a noexecute attribute.&lt;br /&gt;
* Add the following to &amp;quot;execute a script block&amp;quot;, before &amp;quot;1. Initialize the script block&#039;s source as follows&amp;quot;:&lt;br /&gt;
  0. If the element has a &amp;quot;noexecute&amp;quot; attribute, set the &amp;quot;pending-execution&amp;quot; flag&lt;br /&gt;
  and abort these steps.&lt;br /&gt;
* For each part of step 15 &amp;quot;Then, the first ...&amp;quot; of &amp;quot;prepare a script&amp;quot;, the noexecute attribute being set implies the async attribute is also set, eg. &amp;quot;... and the element has neither an async nor a noexecute attribute&amp;quot;.  That is, noexecute implies async.&lt;br /&gt;
* If the &amp;quot;noexecute&amp;quot; attribute is changed, and the &amp;quot;pending-execution&amp;quot; flag is set, clear the &amp;quot;pending-execution&amp;quot; flag and queue a task to execute the script block.&lt;br /&gt;
* When the fetch algorithm completes without error, fire a simple event named &amp;quot;fetch&amp;quot; at the element.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
&lt;br /&gt;
* Invariant: if the &amp;quot;pending-execution&amp;quot; flag is set, the &amp;quot;already started&amp;quot; flag is also set.&lt;br /&gt;
* The attribute name &amp;quot;noexecute&amp;quot; reflects its behavior more clearly than &amp;quot;preload&amp;quot;.  API naming should reflect what the API does, not name a use case.&lt;br /&gt;
* For feature detection: typeof(document.createElement(&amp;quot;script&amp;quot;).noexecute) == &amp;quot;boolean&amp;quot;.&lt;br /&gt;
* Scripts are still only executed once, regulated by the &amp;quot;already started&amp;quot; flag.&lt;br /&gt;
* The &amp;quot;pending-execution&amp;quot; flag is not copied to clones, so a script that has run &amp;quot;prepare a script&amp;quot; will run again in a clone.&lt;br /&gt;
* The onfetch event is orthogonal to the rest, and should be fired whether or not noexecute is set.  It&#039;s symmetric with onerror.  (onreadystatechange is not recommended; it clashes with existing IE behavior.  IE fires onreadystatechange with &amp;quot;loaded&amp;quot; for both success and error, making it hard to tell whether the load actually succeeded.)  The &amp;quot;fetch&amp;quot; event isn&#039;t a new concept, it&#039;s simply the &amp;quot;load&amp;quot; of Progress Events-like contexts; a different name is used because script events already have a &amp;quot;load&amp;quot; event with a different meaning.&lt;br /&gt;
* &amp;lt;script noexecute&amp;gt; is comparable to &amp;lt;style disabled&amp;gt;, which can also be used for advance resource loading.&lt;br /&gt;
* The fetch event should be fired at most once, if the fetch algorithm finishes without error.  That&#039;s not the top of &amp;quot;execute a script block&amp;quot; since that can be executed more than once.  I&#039;m not sure what the correct spec mechanism is to describe this.&lt;br /&gt;
* This could be extended slightly to support inline scripts, with additional modifications to &amp;quot;prepare a script&amp;quot; step 15.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
* Feedback is that this approach is more likely to see implementation in Gecko, and it avoids potential issues related to fetching resources when not added to a document without needing additional resource limiting.&lt;br /&gt;
* DOM event consistency is maintained by not requiring elements be kept out of the document while loading.  Fetch and error events can be captured, so event delegation continues to work consistently.&lt;br /&gt;
* Fetch error handling (DNS errors, 404 errors) is handled with no changes to current behavior.&lt;br /&gt;
* In addition to programmatic preloading, scripts can also be preloaded in markup and later activated by scripts.&lt;br /&gt;
&lt;br /&gt;
--[[Talk:Script_Execution_Control#Discussion_for_2.3_Proposal|discussion]]&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. From the markup perspective, this approach does not have graceful degradation, since in browsers without `noexecute` support, such scripts would execute right away, even though told not to. Markup doesn&#039;t provide the ability to feature-detect.&lt;br /&gt;
* [[Talk:Script_Execution_Control#Discussion_for_2.3_Proposal|discussion]]&lt;br /&gt;
&lt;br /&gt;
2. As stated in support sections for the other proposals, this proposal requires a fundamental change to how IE operates. As such, the feasibility of getting such a change into a released version of IE is, at best 18-24 months down the road, and that&#039;s if IE even agreed to change their existing preloading behavior. &lt;br /&gt;
* [[Talk:Script_Execution_Control#Discussion_for_2.3_Proposal|discussion]]&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Talk:Script_Execution_Control&amp;diff=6305</id>
		<title>Talk:Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Talk:Script_Execution_Control&amp;diff=6305"/>
		<updated>2011-03-06T19:33:41Z</updated>

		<summary type="html">&lt;p&gt;Getify: further discussions over proposal 2.3&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== Discussion for [[Script_Execution_Control#Limitations_2|2.2.1.4 Limitations]] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Events (eg. onload) fired on preloading script elements can not be captured by the document, because the node is not in the DOM tree during preloading.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* true, but this seems like pretty inconsequential. no valid use-case has been presented where this type of event handling would be superior than element-binding event handling, for the use-cases under consideration. [[User:Getify|Getify]] 23:31, 3 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
* This breaks from a fairly consistent API design across DOM elements; that&#039;s not inconsequential.  The use cases are the same as any event capturing (eg. event delegation, logging).  [[User:Glenn|Glenn]]&lt;br /&gt;
&lt;br /&gt;
* But almost every script loader I&#039;ve ever seen or heard of uses direct script element event binding, not DOM event propagation, to listen for &amp;quot;onload&amp;quot; or &amp;quot;onreadystatechange&amp;quot;. I&#039;m only saying that it&#039;s trivial because this is not how script loaders do it, and so it&#039;s not a necessary use-case for the &amp;quot;preloading&amp;quot; that script loaders like LABjs or ControlJS want to do. What you&#039;re suggesting *could* have validity as extra benefit, but it&#039;s unfair to label it as a limitation or detraction from these 2 proposals, because in all fairness, noone&#039;s actually doing that now with any of the script load events. Just write up your third proposal, and include DOM event propagation as an additional benefit. [[User:Getify|Getify]] 19:19, 4 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
* We&#039;re lucky enough to have relative consistency across the DOM API in being able to capture events.  Being able to capture onerror at the document to globally receive all errors is extremely useful, for example.  Reducing this consistency is not a trivial issue.  [[User:Glenn|Glenn]]&lt;br /&gt;
&lt;br /&gt;
=== Discussion for [[Script_Execution_Control#Proposal_3_.28Glenn_Maynard.29|2.3 Proposal]] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Feedback is that this approach is more likely to see implementation in Gecko, and it avoids potential issues related to fetching resources when not added to a document without needing additional resource limiting.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Not sure if this is a fully accurate statement, based on the WHATWG thread. Boris&#039; most recent comments indicate perhaps the opposite. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
* I&#039;ll leave him to comment for himself.  [[User:Glenn|Glenn]]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Fetch error handling (DNS errors, 404 errors) is handled with no changes to current behavior.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* There&#039;s lots of fetch error conditions not currently handled consistently cross-browser by `onerror`, like 403&#039;s and 500&#039;s for instance. The error mechanism is fundamentally broken/under-specified when it comes to loading/network errors. So, citing a broken/under-implemented mechanism as support for this proposal is rather moot. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
* Are you proposing that the onerror event for script be dropped?  If not, what exactly are you proposing?  This is a contrived argument against a straightforward benefit: error handling is clearly defined under this proposal.  [[User:Glenn|Glenn]]&lt;br /&gt;
* I&#039;m not proposing it be dropped (right now). I&#039;m simply asserting that because it&#039;s current implementation specifically for catching *network* loading errors is quite incomplete (only catches 1 error code -- 404 -- consistently cross-browser, out of a dozen or more other load errors that can and do happen), for preloading specifically, `onerror` won&#039;t be much help, since all preloading errors would only be loading errors. Since `onerror` is grossly under-implemented for network errors, it&#039;s not really a strong supporting case that your proposal preserves such an impractical mechanism. As the author of a wide-spread general script loader library (LABjs), I can say that `onerror` incompleteness and inconsistency cross-browser makes it totally unusable in a real world scenario (what good is a script loader &#039;error-detection&#039; API that can only detect a small subset of the possible errors?). [[User:Getify|Getify]] 19:33, 6 March 2011 (UTC)&lt;br /&gt;
* Now, if you&#039;re ALSO proposing that `onerror` be completely fixed so it handles all types of network errors, and you have explored the feasibility of how all browsers can actually detect such errors given peculiarities of how the loading mechanism deals with the HTTP request stack, and you&#039;re going to clearly spell out all those semantics and details, fine. But your proposal just rests some laurels on &amp;quot;look, it&#039;s supports `onerror` handling&amp;quot;, and my assertion is that `onerror` is pretty lame at the current time. I&#039;d much rather attack the problems with `onerror` (or rather, just general load-error-detection) on script elements in a completely separate proposal cycle, one which I may very well suggest that `onerror` just be dropped altogether in favor of a new and more reliable mechanism. [[User:Getify|Getify]] 19:33, 6 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
In addition to programmatic preloading, scripts can also be preloaded in markup and later activated by scripts.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Again, noone has yet expressed any serious use-cases for how that would be practical or more useful compared to programmatic preloading. Early in the WHATWG thread it was brought up, and never really went anywhere. Since you&#039;d have to have some &amp;quot;script loader&amp;quot;-like logic to inspect the DOM for preloaded-and-waiting scripts and execute them in the proper order anyway, why not just have only a script loader? No benefit has been shown to having these script elements in the parser markup rather than programmatically injected. Optimizing the process for how all script loaders currently work is generally not a bad appraoch, so I&#039;m not sure why introducing unprecedented markup-driven preloading is useful. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
* This is obvious: you can put dependent scripts in markup in the traditional way, allow them to be loaded normally on browsers that don&#039;t support noexecute, and execute them on demand on ones that do.  [[User:Glenn|Glenn]]&lt;br /&gt;
* No, it&#039;s not obvious, nor is it valid. Your argument is like saying that it&#039;d be ok if the &amp;amp;lt;link rel=prefetch&amp;gt; tag was to go ahead and execute scripts, that this would be ok. &amp;quot;Preloading&amp;quot; as a use-case is not just about early (vs. late) loading/execution of a script, it&#039;s specifically about separating loading from execution (aka, preventing auto-execution), and deferring until a later, controlled time, the execution of that prior loaded resource. The markup use-case does NOT degrade gracefully at all for this purpose, because I as an author have no way to guarantee that the script doesn&#039;t execute right away. If I&#039;m trying to do &amp;quot;preloading&amp;quot;, it&#039;s because I have a good reason for it NOT to execute right then. [[User:Getify|Getify]] 19:33, 6 March 2011 (UTC)&lt;br /&gt;
*For instance, say I want to preload 3 different calendar widget scripts, but based on user actions, I only want one to be executed, whenever they click on some control. I certainly can&#039;t tolerate, nor is that graceful degradation, if in non-supporting browsers, all 3 scripts just automatically execute. The only sensible and practical way to handle browsers that don&#039;t support official preloading is to use the fallback &amp;quot;cache preloading&amp;quot; hacks (like the fake mime-type, the &amp;amp;lt;object&amp;gt;, or the Image). This *has* to be done via script logic. Therefore, my assertion, the main use-case for preloading is NOT served, nor gracefully degraded, by markup driven preloading. [[User:Getify|Getify]] 19:33, 6 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Limitations&lt;br /&gt;
&lt;br /&gt;
From the markup perspective, this approach does not have graceful degradation, since in browsers without `noexecute` support, such scripts would execute right away, even though told not to. Markup doesn&#039;t provide the ability to feature-detect.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Since the markup use-case doesn&#039;t degrade well, and markup doesn&#039;t provide feature-detection to handle such lack of support in an alternate way, the markup use-case for preloading is moot and unuseable in practice. This is further reasoning for why &amp;quot;preloading&amp;quot; should only be controlled by a script loader injecting the script elements dynamically, which the other proposals handle quite well. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
* The markup case degrades straightforwardly, loading the scripts normally and in order.  Typically you&#039;d also combine this with defer, for browsers that support it.  [[User:Glenn|Glenn]]&lt;br /&gt;
* You&#039;re focusing on one small niche usage for &amp;quot;preloading&amp;quot;, and missing the main use-case, which is to completely separate loading from execution, and programatically control execution later. The degradation to non-supporting browsers in the markup case would completely violate that fundamental assumption, and, as described just above, could cause script code to execute that the author does not want to execute. This is not graceful degradation. It&#039;s utter failure of the main use-case. The markup use-case for &amp;quot;preloading&amp;quot; is weak in and of itself, and it totally does not degrade for the main intended uses of &amp;quot;preloading&amp;quot; that precipitated these proposals in the first place. [[User:Getify|Getify]] 19:33, 6 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
As stated in support sections for the other proposals, this proposal requires a fundamental change to how IE operates. As such, the feasibility of getting such a change into a released version of IE is, at best 18-24 months down the road, and that&#039;s if IE even agreed to change their existing preloading behavior.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* IE may rightly say that they already support preloading, and willfully violate such a proposal (even if spec&#039;d), which will continue to keep us in non-compat browser forking instead of convergent compat. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
* They may say a lot of things.  You stretching the &amp;quot;IE card&amp;quot; very thin.  [[User:Glenn|Glenn]]&lt;br /&gt;
* I don&#039;t feel that the pragmatism of keeping IE&#039;s perspective (and ability and willingness to change LONG-HELD precedent in their browser) is &amp;quot;stretching&amp;quot;... I think it&#039;s wise and prudent compromise process. But I suppose we&#039;ll just have to disagree on that. [[User:Getify|Getify]] 19:33, 6 March 2011 (UTC)&lt;br /&gt;
* And I say &amp;quot;compromise&amp;quot; intentionally, because I actually prefer Nicholas&#039; current (v2.1) proposal to mine... except for the fact that I think it has much less chance of finding compat across all browsers any time soon. I&#039;m making the compromises in my proposal based on the idea that it&#039;s better to have a mechanism that&#039;s not quite ideal but works, if IE and all other browsers can agree, than to have a mechanism that&#039;s perfectly ideal and semantic but which will be rejected by one or more browser vendors. You on the other hand seem to dismiss outright the validity of considering IE&#039;s perspective on this issue. [[User:Getify|Getify]] 19:33, 6 March 2011 (UTC)&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6301</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6301"/>
		<updated>2011-03-04T23:56:33Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding limitations for proposal 3, as well as links to discussion&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
4. Run-away memory usage (see [[Script_Execution_Control#Limitations_2|2.2.1.3]])&lt;br /&gt;
&lt;br /&gt;
5. Doesn&#039;t support inline script &amp;quot;preloading&amp;quot; (see [[Script_Execution_Control#Limitations_2|2.2.1.2]])&lt;br /&gt;
&lt;br /&gt;
6. This proposal doesn&#039;t specify fetch error handling.  onerror is currently not fired on scripts which are not in a document.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
* a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
5. This proposal doesn&#039;t specify fetch error handling.  onerror is currently not fired on scripts which are not in a document.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 3 (Glenn Maynard) === &lt;br /&gt;
&lt;br /&gt;
This proposal keeps changes to script preparation and execution to a bare minimum.  Changes are described in terms of actual spec changes, to describe its behavior clearly and to show how it fits in with the existing spec.  Briefly, a &amp;quot;noexecute&amp;quot; attribute is added to block execution of the script, which is cleared to reenable execution.  More precisely:&lt;br /&gt;
&lt;br /&gt;
* Add the following attribute definition:&lt;br /&gt;
  The noexecute attribute is a boolean attribute that indicates that the&lt;br /&gt;
  script should not be executed.&lt;br /&gt;
* Add the following flag definition:&lt;br /&gt;
  The fifth is a flag indicating that a script is &amp;quot;pending-execution&amp;quot;.  Initially,&lt;br /&gt;
  script elements must have this flag unset.  This flag is used to trigger execution&lt;br /&gt;
  if execution was deferred due to a noexecute attribute.&lt;br /&gt;
* Add the following to &amp;quot;execute a script block&amp;quot;, before &amp;quot;1. Initialize the script block&#039;s source as follows&amp;quot;:&lt;br /&gt;
  0. If the element has a &amp;quot;noexecute&amp;quot; attribute, set the &amp;quot;pending-execution&amp;quot; flag&lt;br /&gt;
  and abort these steps.&lt;br /&gt;
* For each part of step 15 &amp;quot;Then, the first ...&amp;quot; of &amp;quot;prepare a script&amp;quot;, the noexecute attribute being set implies the async attribute is also set, eg. &amp;quot;... and the element has neither an async nor a noexecute attribute&amp;quot;.  That is, noexecute implies async.&lt;br /&gt;
* If the &amp;quot;noexecute&amp;quot; attribute is changed, and the &amp;quot;pending-execution&amp;quot; flag is set, clear the &amp;quot;pending-execution&amp;quot; flag and queue a task to execute the script block.&lt;br /&gt;
* When the fetch algorithm completes without error, fire a simple event named &amp;quot;fetch&amp;quot; at the element.&lt;br /&gt;
&lt;br /&gt;
Notes:&lt;br /&gt;
&lt;br /&gt;
* Invariant: if the &amp;quot;pending-execution&amp;quot; flag is set, the &amp;quot;already started&amp;quot; flag is also set.&lt;br /&gt;
* The attribute name &amp;quot;noexecute&amp;quot; reflects its behavior more clearly than &amp;quot;preload&amp;quot;.  API naming should reflect what the API does, not name a use case.&lt;br /&gt;
* For feature detection: typeof(document.createElement(&amp;quot;script&amp;quot;).noexecute) == &amp;quot;boolean&amp;quot;.&lt;br /&gt;
* Scripts are still only executed once, regulated by the &amp;quot;already started&amp;quot; flag.&lt;br /&gt;
* The &amp;quot;pending-execution&amp;quot; flag is not copied to clones, so a script that has run &amp;quot;prepare a script&amp;quot; will run again in a clone.&lt;br /&gt;
* The onfetch event is orthogonal to the rest, and should be fired whether or not noexecute is set.  It&#039;s symmetric with onerror.  (onreadystatechange is not recommended; it clashes with existing IE behavior.  IE fires onreadystatechange with &amp;quot;loaded&amp;quot; for both success and error, making it hard to tell whether the load actually succeeded.)  The &amp;quot;fetch&amp;quot; event isn&#039;t a new concept, it&#039;s simply the &amp;quot;load&amp;quot; of Progress Events-like contexts; a different name is used because script events already have a &amp;quot;load&amp;quot; event with a different meaning.&lt;br /&gt;
* &amp;lt;script noexecute&amp;gt; is comparable to &amp;lt;style disabled&amp;gt;, which can also be used for advance resource loading.&lt;br /&gt;
* The fetch event should be fired at most once, if the fetch algorithm finishes without error.  That&#039;s not the top of &amp;quot;execute a script block&amp;quot; since that can be executed more than once.  I&#039;m not sure what the correct spec mechanism is to describe this.&lt;br /&gt;
* This could be extended slightly to support inline scripts, with additional modifications to &amp;quot;prepare a script&amp;quot; step 15.&lt;br /&gt;
&lt;br /&gt;
Advantages:&lt;br /&gt;
&lt;br /&gt;
* Feedback is that this approach is more likely to see implementation in Gecko, and it avoids potential issues related to fetching resources when not added to a document without needing additional resource limiting.&lt;br /&gt;
* DOM event consistency is maintained by not requiring elements be kept out of the document while loading.  Fetch and error events can be captured, so event delegation continues to work consistently.&lt;br /&gt;
* Fetch error handling (DNS errors, 404 errors) is handled with no changes to current behavior.&lt;br /&gt;
* In addition to programmatic preloading, scripts can also be preloaded in markup and later activated by scripts.&lt;br /&gt;
&lt;br /&gt;
--[[Talk:Script_Execution_Control#Discussion_for_2.3_Proposal|discussion]]&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. From the markup perspective, this approach does not have graceful degradation, since in browsers without `noexecute` support, such scripts would execute right away, even though told not to. Markup doesn&#039;t provide the ability to feature-detect.&lt;br /&gt;
* [[Talk:Script_Execution_Control#Discussion_for_2.3_Proposal|discussion]]&lt;br /&gt;
&lt;br /&gt;
2. As stated in support sections for the other proposals, this proposal requires a fundamental change to how IE operates. As such, the feasibility of getting such a change into a released version of IE is, at best 18-24 months down the road, and that&#039;s if IE even agreed to change their existing preloading behavior. &lt;br /&gt;
* [[Talk:Script_Execution_Control#Discussion_for_2.3_Proposal|discussion]]&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Talk:Script_Execution_Control&amp;diff=6300</id>
		<title>Talk:Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Talk:Script_Execution_Control&amp;diff=6300"/>
		<updated>2011-03-04T23:53:56Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding Discussion for 2.3&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== Discussion for [[Script_Execution_Control#Limitations_2|2.2.1.4 Limitations]] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Events (eg. onload) fired on preloading script elements can not be captured by the document, because the node is not in the DOM tree during preloading.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* true, but this seems like pretty inconsequential. no valid use-case has been presented where this type of event handling would be superior than element-binding event handling, for the use-cases under consideration. [[User:Getify|Getify]] 23:31, 3 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
* This breaks from a fairly consistent API design across DOM elements; that&#039;s not inconsequential.  The use cases are the same as any event capturing (eg. event delegation, logging).  [[User:Glenn|Glenn]]&lt;br /&gt;
&lt;br /&gt;
* But almost every script loader I&#039;ve ever seen or heard of uses direct script element event binding, not DOM event propagation, to listen for &amp;quot;onload&amp;quot; or &amp;quot;onreadystatechange&amp;quot;. I&#039;m only saying that it&#039;s trivial because this is not how script loaders do it, and so it&#039;s not a necessary use-case for the &amp;quot;preloading&amp;quot; that script loaders like LABjs or ControlJS want to do. What you&#039;re suggesting *could* have validity as extra benefit, but it&#039;s unfair to label it as a limitation or detraction from these 2 proposals, because in all fairness, noone&#039;s actually doing that now with any of the script load events. Just write up your third proposal, and include DOM event propagation as an additional benefit. [[User:Getify|Getify]] 19:19, 4 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Discussion for [[Script_Execution_Control#Proposal_3_.28Glenn_Maynard.29|2.3 Proposal]] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Feedback is that this approach is more likely to see implementation in Gecko, and it avoids potential issues related to fetching resources when not added to a document without needing additional resource limiting.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Not sure if this is a fully accurate statement, based on the WHATWG thread. Boris&#039; most recent comments indicate perhaps the opposite. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Fetch error handling (DNS errors, 404 errors) is handled with no changes to current behavior.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* There&#039;s lots of fetch error conditions not currently handled consistently cross-browser by `onerror`, like 403&#039;s and 500&#039;s for instance. The error mechanism is fundamentally broken/under-specified when it comes to loading/network errors. So, citing a broken/under-implemented mechanism as support for this proposal is rather moot. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
In addition to programmatic preloading, scripts can also be preloaded in markup and later activated by scripts.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Again, noone has yet expressed any serious use-cases for how that would be practical or more useful compared to programmatic preloading. Early in the WHATWG thread it was brought up, and never really went anywhere. Since you&#039;d have to have some &amp;quot;script loader&amp;quot;-like logic to inspect the DOM for preloaded-and-waiting scripts and execute them in the proper order anyway, why not just have only a script loader? No benefit has been shown to having these script elements in the parser markup rather than programmatically injected. Optimizing the process for how all script loaders currently work is generally not a bad appraoch, so I&#039;m not sure why introducing unprecedented markup-driven preloading is useful. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Limitations&lt;br /&gt;
&lt;br /&gt;
From the markup perspective, this approach does not have graceful degradation, since in browsers without `noexecute` support, such scripts would execute right away, even though told not to. Markup doesn&#039;t provide the ability to feature-detect.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* Since the markup use-case doesn&#039;t degrade well, and markup doesn&#039;t provide feature-detection to handle such lack of support in an alternate way, the markup use-case for preloading is moot and unuseable in practice. This is further reasoning for why &amp;quot;preloading&amp;quot; should only be controlled by a script loader injecting the script elements dynamically, which the other proposals handle quite well. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
As stated in support sections for the other proposals, this proposal requires a fundamental change to how IE operates. As such, the feasibility of getting such a change into a released version of IE is, at best 18-24 months down the road, and that&#039;s if IE even agreed to change their existing preloading behavior.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* IE may rightly say that they already support preloading, and willfully violate such a proposal (even if spec&#039;d), which will continue to keep us in non-compat browser forking instead of convergent compat. [[User:Getify|Getify]] 23:53, 4 March 2011 (UTC)&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6294</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6294"/>
		<updated>2011-03-04T19:33:36Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* Limitations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
4. Run-away memory usage (see [[Script_Execution_Control#Limitations_2|2.2.1.3]])&lt;br /&gt;
&lt;br /&gt;
5. Doesn&#039;t support inline script &amp;quot;preloading&amp;quot; (see [[Script_Execution_Control#Limitations_2|2.2.1.2]])&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
* a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6293</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6293"/>
		<updated>2011-03-04T19:33:02Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* Limitations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
4. Run-away memory usage (see [[Script_Execution_Control#Limitations_2|2.2.1.3]])&lt;br /&gt;
&lt;br /&gt;
5. Doesn&#039;t support inline script &amp;quot;preloading&amp;quot; (see [[Script_Execution_Control#Limitations_2|2.2.1.2]])&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6292</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6292"/>
		<updated>2011-03-04T19:30:23Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding cross-link to other proposal&amp;#039;s limitation description on not supporting inline script &amp;quot;preloading&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
4. Run-away memory usage (see [[Script_Execution_Control#Limitations_2|2.2.1.3]])&lt;br /&gt;
&lt;br /&gt;
5. Doesn&#039;t support inline script &amp;quot;preloading&amp;quot; (see [[Script_Execution_Control#Limitations_2|2.2.1.2]])&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6291</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6291"/>
		<updated>2011-03-04T19:28:08Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding cross-link to other proposal&amp;#039;s limitation description on &amp;quot;run-away memory&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
4. Run-away memory usage (see [[Script_Execution_Control#Limitations_2|2.2.1.3]])&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6290</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6290"/>
		<updated>2011-03-04T19:25:16Z</updated>

		<summary type="html">&lt;p&gt;Getify: updating link&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6289</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6289"/>
		<updated>2011-03-04T19:24:11Z</updated>

		<summary type="html">&lt;p&gt;Getify: updating link&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control#Discussion_for_2.2.1.4_Limitations]])&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control]])&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Talk:Script_Execution_Control&amp;diff=6288</id>
		<title>Talk:Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Talk:Script_Execution_Control&amp;diff=6288"/>
		<updated>2011-03-04T19:23:26Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* Discussion for 2.2.1.4 Limitations: */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== Discussion for [[Script_Execution_Control#Limitations_2|2.2.1.4 Limitations]] ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Events (eg. onload) fired on preloading script elements can not be captured by the document, because the node is not in the DOM tree during preloading.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* true, but this seems like pretty inconsequential. no valid use-case has been presented where this type of event handling would be superior than element-binding event handling, for the use-cases under consideration. [[User:Getify|Getify]] 23:31, 3 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
* This breaks from a fairly consistent API design across DOM elements; that&#039;s not inconsequential.  The use cases are the same as any event capturing (eg. event delegation, logging).  [[User:Glenn|Glenn]]&lt;br /&gt;
&lt;br /&gt;
* But almost every script loader I&#039;ve ever seen or heard of uses direct script element event binding, not DOM event propagation, to listen for &amp;quot;onload&amp;quot; or &amp;quot;onreadystatechange&amp;quot;. I&#039;m only saying that it&#039;s trivial because this is not how script loaders do it, and so it&#039;s not a necessary use-case for the &amp;quot;preloading&amp;quot; that script loaders like LABjs or ControlJS want to do. What you&#039;re suggesting *could* have validity as extra benefit, but it&#039;s unfair to label it as a limitation or detraction from these 2 proposals, because in all fairness, noone&#039;s actually doing that now with any of the script load events. Just write up your third proposal, and include DOM event propagation as an additional benefit. [[User:Getify|Getify]] 19:19, 4 March 2011 (UTC)&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6287</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6287"/>
		<updated>2011-03-04T19:21:05Z</updated>

		<summary type="html">&lt;p&gt;Getify: moving discussion to talk page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control]])&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control]])&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6286</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6286"/>
		<updated>2011-03-04T19:20:29Z</updated>

		<summary type="html">&lt;p&gt;Getify: moving discussion to talk page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. DOM event propagation (discussion moved to: [[Talk:Script_Execution_Control]])&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. Events (eg. onload) fired on preloading script elements can not be captured by the document, because the node is not in the DOM tree during preloading.&lt;br /&gt;
-- true, but this seems like pretty inconsequential. no valid use-case has been presented where this type of event handling would be superior than element-binding event handling, for the use-cases under consideration. [[User:Getify|Getify]] 23:30, 3 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Talk:Script_Execution_Control&amp;diff=6285</id>
		<title>Talk:Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Talk:Script_Execution_Control&amp;diff=6285"/>
		<updated>2011-03-04T19:19:04Z</updated>

		<summary type="html">&lt;p&gt;Getify: moving discussion from the main page to here&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;=== Discussion for [[Script_Execution_Control#Limitations_2|2.2.1.4 Limitations]]: ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Events (eg. onload) fired on preloading script elements can not be captured by the document, because the node is not in the DOM tree during preloading.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* true, but this seems like pretty inconsequential. no valid use-case has been presented where this type of event handling would be superior than element-binding event handling, for the use-cases under consideration. [[User:Getify|Getify]] 23:31, 3 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
* This breaks from a fairly consistent API design across DOM elements; that&#039;s not inconsequential.  The use cases are the same as any event capturing (eg. event delegation, logging).  [[User:Glenn|Glenn]]&lt;br /&gt;
&lt;br /&gt;
* But almost every script loader I&#039;ve ever seen or heard of uses direct script element event binding, not DOM event propagation, to listen for &amp;quot;onload&amp;quot; or &amp;quot;onreadystatechange&amp;quot;. I&#039;m only saying that it&#039;s trivial because this is not how script loaders do it, and so it&#039;s not a necessary use-case for the &amp;quot;preloading&amp;quot; that script loaders like LABjs or ControlJS want to do. What you&#039;re suggesting *could* have validity as extra benefit, but it&#039;s unfair to label it as a limitation or detraction from these 2 proposals, because in all fairness, noone&#039;s actually doing that now with any of the script load events. Just write up your third proposal, and include DOM event propagation as an additional benefit. [[User:Getify|Getify]] 19:19, 4 March 2011 (UTC)&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6283</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6283"/>
		<updated>2011-03-03T23:31:49Z</updated>

		<summary type="html">&lt;p&gt;Getify: &amp;quot;events&amp;quot; limitation, and response&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
3. Events (eg. onload) fired on preloading script elements can not be captured by the document, because the node is not in the DOM tree during preloading.&lt;br /&gt;
-- true, but this seems like pretty inconsequential. no valid use-case has been presented where this type of event handling would be superior than element-binding event handling, for the use-cases under consideration. [[User:Getify|Getify]] 23:31, 3 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. Events (eg. onload) fired on preloading script elements can not be captured by the document, because the node is not in the DOM tree during preloading.&lt;br /&gt;
-- true, but this seems like pretty inconsequential. no valid use-case has been presented where this type of event handling would be superior than element-binding event handling, for the use-cases under consideration. [[User:Getify|Getify]] 23:30, 3 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6282</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6282"/>
		<updated>2011-03-03T23:30:12Z</updated>

		<summary type="html">&lt;p&gt;Getify: response to &amp;quot;events&amp;quot; limitation&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
4. Events (eg. onload) fired on preloading script elements can not be captured by the document, because the node is not in the DOM tree during preloading.&lt;br /&gt;
-- true, but this seems like pretty inconsequential. no valid use-case has been presented where this type of event handling would be superior than element-binding event handling, for the use-cases under consideration. [[User:Getify|Getify]] 23:30, 3 March 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6279</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6279"/>
		<updated>2011-03-03T22:33:52Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* Limitations */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
-- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6278</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6278"/>
		<updated>2011-03-03T22:30:36Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding discussion of &amp;quot;run-away memory&amp;quot; issue&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
3. All the preloading proposals suffer a potential for run-away memory consumption if a script misbehaves and causes a bunch of &amp;quot;preloaded&amp;quot; scripts that it abandons and never attaches to the page to execute. The browser would have to hold on to that memory for the lifetime of the page, assuming that one might be used at some later time on the page.&lt;br /&gt;
&lt;br /&gt;
This particular proposal makes this a little bit more likely since it&#039;s easier (automatic, rather than opt-in) to make the mistake.&lt;br /&gt;
&lt;br /&gt;
Two suggestions for how to address:&lt;br /&gt;
  a) implement some content-uniqueness in-memory cache which only keeps one copy of each unique set of resource content in memory, to prevent needless duplication. the algorithm for how to do this correctly is probably quite complicated to actually do.&lt;br /&gt;
&lt;br /&gt;
  b) implement a cap on the number of scripts that will be preloaded, for instance at 200 or 500. If the cap is high enough, almost all normal sites which will use preloading will never need that many scripts waiting in a preloading queue. And the run-away sites will be prevented from consuming more than that cap. Any script preload requests above the cap will simply sit and wait for a preloading slot to become available. The resolution is to simply start executing some scripts by adding them to the page, thereby freeing up more preloading slots.&lt;br /&gt;
&lt;br /&gt;
    -- a sub-idea for this suggestion is to have, instead of a hard-cap, a soft-cap, where if it&#039;s hit, a user-prompt happens (like the &amp;quot;long-running JavaScript&amp;quot; prompt), where a user can decide if the browser should be allowed to keep going with preloading or be capped.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=6260</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=6260"/>
		<updated>2011-02-21T14:51:13Z</updated>

		<summary type="html">&lt;p&gt;Getify: bolding the update text&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;*Update: The main proposal listed here, also known as &amp;quot;async=false&amp;quot;, was recently [http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 officially adopted] [http://ln.hixie.ch/?start=1296711456&amp;amp;count=1 by Ian Hixie] into the HTML Specification.&#039;&#039;&#039;&lt;br /&gt;
[[User:Getify|Getify]] 00:31, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, &amp;lt;del&amp;gt;and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&amp;lt;/del&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Update: the behavior *is* in fact in the spec as a suggestion (aka, with &amp;quot;may&amp;quot; instead of &amp;quot;should&amp;quot;). The wording in question is located in [http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML5 4.3.1, &amp;quot;Running A Script&amp;quot; algorithm] step, 12, which states:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#f1dac9&amp;quot;&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 19:32, 23 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be [[Dynamic_Script_Execution_Order#readyState_.22preloading.22|listed as such]]. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=620852 Mozilla Bug #620852]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
If at any point these services begin dynamically loading `async=false` scripts of their own, they will begin blocking.  This can lead to unexpected behavior of existing code as third-party libraries and services change.  It would seem the specification should provide a means by which this blocking can be isolated to dependencies defined by the author and ordered execution not introduce conditions that allow unrelated libraries to affect this.   &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Need Attribute/Property Semantic Inconsistency ====&lt;br /&gt;
In order to achieve consistency between the semantics of how the parser-inserted script element&#039;s `async` attribute and the script-inserted script element&#039;s `async` property behave, a slight change needs to be made to the value parsing/interpretation for parser-inserted script elements&#039; `async` attribute.&lt;br /&gt;
&lt;br /&gt;
If a parser-inserted script element has the `async` attribute present, but its value is exactly &amp;quot;false&amp;quot; (or any capitalization thereof), the script element should behave EXACTLY as if no `async` attribute were present.&lt;br /&gt;
&lt;br /&gt;
If the attribute is present and either has no value, or has any other value except &amp;quot;false&amp;quot; (for instance, &amp;quot;true&amp;quot; or &amp;quot;async&amp;quot;), then it should be interpreted to be turning on &amp;quot;async&amp;quot; for that element.&lt;br /&gt;
&lt;br /&gt;
Because it is highly unlikely (and indeed, quite irrational) that any existing web content is doing `async=&amp;quot;false&amp;quot;` in HTML markup and yet still expecting &amp;quot;async&amp;quot; behavior to be turned on, it is pretty unlikely that this minor interpretation change will cause backwards-compatibility issues.&lt;br /&gt;
[[User:Getify|Getify]] 03:05, 22 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
And does the onload event fire again after the script executes?  &amp;lt;br/&amp;gt; &lt;br /&gt;
Or only when it&#039;s loaded? &amp;lt;br/&amp;gt; &lt;br /&gt;
Both seem to violate current `onload` semantics.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 20:27, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
As far as I&#039;m concerned, I think the &amp;quot;load&amp;quot; event should happen immediately after a script loads, but before it executes, as I spell out in the [[Dynamic_Script_Execution_Order#Proposal_Amendment:_Events|Events proposal ammendment section]]. [[User:Getify|Getify]] 20:33, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question. This alternate proposal is very similar in spirit to the [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|previous section on explicit support for &amp;quot;text/cache&amp;quot;]] mime-type value.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Update&#039;&#039;&#039; [[User:Getify|Getify]] 14:10, 20 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This seems to be the spec stating that browsers may do what IE&#039;s behavior on scripts does, which is to start fetching the resource once the `src` property is set, but not execute it until the script element is inserted into the DOM.&lt;br /&gt;
&lt;br /&gt;
It would seem then that browsers could/should be petitioned to consider implementing this suggestion, perhaps taking the `readyState` implementation from IE as guidance. While I&#039;m still not sure that this would be my preferred method of solving the overall use-case in discussion on this page, it would be an option for such, and it would also help other use-cases, such as those being discussed in this W3C public-html thread:&lt;br /&gt;
&lt;br /&gt;
[http://lists.w3.org/Archives/Public/public-html/2010Dec/0174.html Need to: &amp;quot;preload&amp;quot; CSS and JS]&lt;br /&gt;
&lt;br /&gt;
==== Do nothing and encourage vendors implement the existing suggestion ====&lt;br /&gt;
As noted by Getify above, the spec all ready makes the suggestion that a script&#039;s SRC attribute be fetched immediately upon assignment.  &lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
While not explicit, it seems the specification all ready deals with the use-case this is hoping to achieve: It makes parallel loading scripts &#039;&#039;&#039;optional&#039;&#039;&#039;.   Execution order can all ready be handled by loaders using onload-chaining.  Therefore, parallel loading can be achieved, but only in browsers that have chosen to implement the suggestion. &lt;br /&gt;
&lt;br /&gt;
Given this, the discussion can shift to: &lt;br /&gt;
# Should the suggestion be changed to a mandate? &lt;br /&gt;
# Should the specification include a way to &#039;&#039;&#039;force&#039;&#039;&#039; parallel loading? &lt;br /&gt;
# Should we be petitioning vendors, not the the W3C, to implement the existing suggestion? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6239</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6239"/>
		<updated>2011-02-14T15:31:59Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding simple formatting&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6238</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6238"/>
		<updated>2011-02-14T15:27:13Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding note about IE compat&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;del&amp;gt;2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
2. This proposal requires changes from IE to achieve full-compat. As IE is known to have a much slower release timeline, and IE9 is now feature-complete and nearing a full release soon, the likelihood is that this current IE cycle has been missed for any such changes. That means full-compat would probably be at best 1-2 years from now.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:27, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6237</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6237"/>
		<updated>2011-02-14T15:24:34Z</updated>

		<summary type="html">&lt;p&gt;Getify: updating to reflect the current state of the proposal&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. &amp;lt;del&amp;gt;In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&amp;lt;/del&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 15:24, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6228</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6228"/>
		<updated>2011-02-14T04:47:41Z</updated>

		<summary type="html">&lt;p&gt;Getify: link title correction&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6227</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6227"/>
		<updated>2011-02-14T04:39:09Z</updated>

		<summary type="html">&lt;p&gt;Getify: updating links with new page title&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Order_Control&amp;diff=6226</id>
		<title>Script Execution Order Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Order_Control&amp;diff=6226"/>
		<updated>2011-02-14T04:37:47Z</updated>

		<summary type="html">&lt;p&gt;Getify: moved Script Execution Order Control to Script Execution Control: The word &amp;quot;Order&amp;quot; in the title was slightly misleading to the main intent of this page and proposal&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;#REDIRECT [[Script Execution Control]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6225</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6225"/>
		<updated>2011-02-14T04:37:47Z</updated>

		<summary type="html">&lt;p&gt;Getify: moved Script Execution Order Control to Script Execution Control: The word &amp;quot;Order&amp;quot; in the title was slightly misleading to the main intent of this page and proposal&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Order_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6224</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6224"/>
		<updated>2011-02-14T04:32:10Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding a few of the insufficient solutions&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Order_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
&lt;br /&gt;
=== &amp;amp;lt;link rel=prefetch&amp;gt; ===&lt;br /&gt;
One suggestion in the thread, which has come up numerous times, is that the already spec&#039;d &amp;amp;lt;link rel=prefetch&amp;gt; mechanism could address the use-case.&lt;br /&gt;
&lt;br /&gt;
There are a few problems with this idea.&lt;br /&gt;
&lt;br /&gt;
1. &amp;amp;lt;link rel=prefetch&amp;gt; is currently only defined for &amp;amp;lt;link&amp;gt; tags in markup. The main use-case is specifically about script loaders needing functionality from dynamica script-created script elements.&lt;br /&gt;
&lt;br /&gt;
2. `prefetch` is defined specifically as a &amp;quot;hint&amp;quot; to the browser that it might benefit from loading a resource ahead of time. The description makes it clear the intended use is for preloading resources which may be used on another page. That&#039;s a poor fit for this use-case, because the script loader needs a specific way to say to the browser &amp;quot;I need this resource to load now, because I&#039;m definitely going to use it later&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
3. Once an element would be loaded via &amp;amp;lt;link rel=prefetch&amp;gt;, the question arises as to how that resource can later be executed. There were two main options:&lt;br /&gt;
&lt;br /&gt;
* Add an `execute()` method to the &amp;amp;lt;link&amp;gt; element, that can be used to execute a script that was preloaded. This is problematic because of all the complicated functionality around script elements and their content&#039;s execution, and would require basically copying all those rules over to the &amp;amp;lt;link&amp;gt; element, or at the very least morphing the &amp;amp;lt;link&amp;gt; element into a &amp;amp;lt;script&amp;gt; element at time of execution. This is most likely more trouble than it&#039;s worth.&lt;br /&gt;
&lt;br /&gt;
* Simply re-request the resource at desired time of execution with a proper &amp;amp;lt;script&amp;gt; element container. The problem here is that then the solution becomes just as fatally assumptive as the &amp;quot;cache preloading&amp;quot; tricks referred to earlier in this page. If the resource didn&#039;t/couldn&#039;t properly cache, the second request would result in a costly double-load. There&#039;s simply no way to guarantee that a resource can cache so that a second request can use it directly.&lt;br /&gt;
&lt;br /&gt;
4. The link element does not currently have a well-defined event system that could inform a web author of when the &amp;quot;prefetch&amp;quot; had finished. As is clear from both the main proposals, an event mechanism is crucial for fulfilling the more advanced applications of the main use-case, and the whole system is not worth proposing without such an event notification.&lt;br /&gt;
&lt;br /&gt;
=== all script content must be adjusted not to auto-execute ===&lt;br /&gt;
It has been suggested numerous times in the discussion thread that the simplest solution is for a script author to re-arrange the way a script is written, such that is has no directly executing functionality in its main code section. That way, the script is &amp;quot;safe&amp;quot; to execute (since it does nothing), and its functionality can easily be called upon at later times when actual &amp;quot;execution&amp;quot; is desired.&lt;br /&gt;
&lt;br /&gt;
This argument misses the true nature of the use-case and proposal in two ways:&lt;br /&gt;
&lt;br /&gt;
1. In the mobile-device world, especially, it has been observed that the mere &amp;quot;parsing&amp;quot; (that is, interpreting of a block of code for its function declarations) of a large chunk of code can be so CPU intensive as to overwhelm the device and cause slow-downs in the UI/UX, or even freezes for brief periods of time. The Gmail Mobile team for instance [http://googlecode.blogspot.com/2009/09/gmail-for-mobile-html5-series-reducing.html famously used the /* ... */ comment hack] to hide code from the engine, which drastically reduced their UI latency issues on some mobile devices. &lt;br /&gt;
&lt;br /&gt;
Many other experiments of this nature have been conducted, and the conclusion is that deferring the interpretation-execution of the script is critical to the use-case. So re-writing the script would do no good, unless it were to involve such hacks as the comment hack, which have obvious draw-backs to both the developer and the browser&#039;s caching/optimization efforts.&lt;br /&gt;
&lt;br /&gt;
2. The proposal is an attempt to address real-world use-cases on existing sites and using existing standard script libraries (things like jQuery for instance). It is not feasible to suggest that all content which needs execution-deferral must be subject to fundamental API re-writes. If arbitrary JavaScript code in use on today&#039;s web cannot be made to download and execute in a more performant way without re-writes, then the whole intent of this proposal (and the greater web optimization community) is a bust.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=6223</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=6223"/>
		<updated>2011-02-14T00:31:49Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding note about adoption into the spec&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
*Update: The main proposal listed here, also known as &amp;quot;async=false&amp;quot;, was recently [http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 officially adopted] [http://ln.hixie.ch/?start=1296711456&amp;amp;count=1 by Ian Hixie] into the HTML Specification.&lt;br /&gt;
[[User:Getify|Getify]] 00:31, 14 February 2011 (UTC)&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, &amp;lt;del&amp;gt;and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&amp;lt;/del&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Update: the behavior *is* in fact in the spec as a suggestion (aka, with &amp;quot;may&amp;quot; instead of &amp;quot;should&amp;quot;). The wording in question is located in [http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML5 4.3.1, &amp;quot;Running A Script&amp;quot; algorithm] step, 12, which states:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#f1dac9&amp;quot;&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 19:32, 23 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be [[Dynamic_Script_Execution_Order#readyState_.22preloading.22|listed as such]]. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=620852 Mozilla Bug #620852]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
If at any point these services begin dynamically loading `async=false` scripts of their own, they will begin blocking.  This can lead to unexpected behavior of existing code as third-party libraries and services change.  It would seem the specification should provide a means by which this blocking can be isolated to dependencies defined by the author and ordered execution not introduce conditions that allow unrelated libraries to affect this.   &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Need Attribute/Property Semantic Inconsistency ====&lt;br /&gt;
In order to achieve consistency between the semantics of how the parser-inserted script element&#039;s `async` attribute and the script-inserted script element&#039;s `async` property behave, a slight change needs to be made to the value parsing/interpretation for parser-inserted script elements&#039; `async` attribute.&lt;br /&gt;
&lt;br /&gt;
If a parser-inserted script element has the `async` attribute present, but its value is exactly &amp;quot;false&amp;quot; (or any capitalization thereof), the script element should behave EXACTLY as if no `async` attribute were present.&lt;br /&gt;
&lt;br /&gt;
If the attribute is present and either has no value, or has any other value except &amp;quot;false&amp;quot; (for instance, &amp;quot;true&amp;quot; or &amp;quot;async&amp;quot;), then it should be interpreted to be turning on &amp;quot;async&amp;quot; for that element.&lt;br /&gt;
&lt;br /&gt;
Because it is highly unlikely (and indeed, quite irrational) that any existing web content is doing `async=&amp;quot;false&amp;quot;` in HTML markup and yet still expecting &amp;quot;async&amp;quot; behavior to be turned on, it is pretty unlikely that this minor interpretation change will cause backwards-compatibility issues.&lt;br /&gt;
[[User:Getify|Getify]] 03:05, 22 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
And does the onload event fire again after the script executes?  &amp;lt;br/&amp;gt; &lt;br /&gt;
Or only when it&#039;s loaded? &amp;lt;br/&amp;gt; &lt;br /&gt;
Both seem to violate current `onload` semantics.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 20:27, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
As far as I&#039;m concerned, I think the &amp;quot;load&amp;quot; event should happen immediately after a script loads, but before it executes, as I spell out in the [[Dynamic_Script_Execution_Order#Proposal_Amendment:_Events|Events proposal ammendment section]]. [[User:Getify|Getify]] 20:33, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question. This alternate proposal is very similar in spirit to the [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|previous section on explicit support for &amp;quot;text/cache&amp;quot;]] mime-type value.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Update&#039;&#039;&#039; [[User:Getify|Getify]] 14:10, 20 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This seems to be the spec stating that browsers may do what IE&#039;s behavior on scripts does, which is to start fetching the resource once the `src` property is set, but not execute it until the script element is inserted into the DOM.&lt;br /&gt;
&lt;br /&gt;
It would seem then that browsers could/should be petitioned to consider implementing this suggestion, perhaps taking the `readyState` implementation from IE as guidance. While I&#039;m still not sure that this would be my preferred method of solving the overall use-case in discussion on this page, it would be an option for such, and it would also help other use-cases, such as those being discussed in this W3C public-html thread:&lt;br /&gt;
&lt;br /&gt;
[http://lists.w3.org/Archives/Public/public-html/2010Dec/0174.html Need to: &amp;quot;preload&amp;quot; CSS and JS]&lt;br /&gt;
&lt;br /&gt;
==== Do nothing and encourage vendors implement the existing suggestion ====&lt;br /&gt;
As noted by Getify above, the spec all ready makes the suggestion that a script&#039;s SRC attribute be fetched immediately upon assignment.  &lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
While not explicit, it seems the specification all ready deals with the use-case this is hoping to achieve: It makes parallel loading scripts &#039;&#039;&#039;optional&#039;&#039;&#039;.   Execution order can all ready be handled by loaders using onload-chaining.  Therefore, parallel loading can be achieved, but only in browsers that have chosen to implement the suggestion. &lt;br /&gt;
&lt;br /&gt;
Given this, the discussion can shift to: &lt;br /&gt;
# Should the suggestion be changed to a mandate? &lt;br /&gt;
# Should the specification include a way to &#039;&#039;&#039;force&#039;&#039;&#039; parallel loading? &lt;br /&gt;
# Should we be petitioning vendors, not the the W3C, to implement the existing suggestion? &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6222</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6222"/>
		<updated>2011-02-14T00:24:20Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* External Discussions of this Feature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Order_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
TODO: fill out this section with the other suggestions that have been discussed but deemed insufficient for the use-case.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6221</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6221"/>
		<updated>2011-02-14T00:23:33Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding link to &amp;quot;ControlJS&amp;quot; blog posts&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Order_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.stevesouders.com/blog/2010/12/15/controljs-part-2/ Steve Souders&#039; ControlJS]&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
TODO: fill out this section with the other suggestions that have been discussed but deemed insufficient for the use-case.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6220</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6220"/>
		<updated>2011-02-13T23:23:18Z</updated>

		<summary type="html">&lt;p&gt;Getify: link edits&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Order_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|Proposal 1]]&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
TODO: fill out this section with the other suggestions that have been discussed but deemed insufficient for the use-case.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6219</id>
		<title>Script Execution Control</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Script_Execution_Control&amp;diff=6219"/>
		<updated>2011-02-13T23:18:47Z</updated>

		<summary type="html">&lt;p&gt;Getify: creating initial page&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around the ability to control (aka, defer until desired) the execution of a script, to occur sometime after the script finishes loading.&lt;br /&gt;
&lt;br /&gt;
This feature, with two main proposals for how to accomplish it, has been discussed on in a long email thread on the WHATWG list: [http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot;]&lt;br /&gt;
&lt;br /&gt;
[http://nczonline.net Nicholas Zakas] began the WHATWG thread with his proposal, which he has since refined and is now in version 2.1 at: [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2].&lt;br /&gt;
&lt;br /&gt;
This page is being created to document that first proposal, as well as a second proposal (which actually came as a result of feedback on another WHATWG wiki page/proposal: [[Dynamic_Script_Execution_Order]]) for the same functionality. These are the two main solutions being discussed at the moment. This page will also attempt to distill some of the feedback and other (unfortunately unsuitable) proposals discussed thus far.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Script_Execution_Order_Control]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
The use-case is that one or more scripts may be necessary to download, but not to automatically execute. The reasons for wanting to defer the execution of a script element until on-demand later are varied. They include performance issues with limited CPU&#039;s on mobile devices, the ability to choose based on user-action which of two scripts should be applied, the ability to delay the execution of a script until the user-action activates some part of a page, etc.&lt;br /&gt;
&lt;br /&gt;
In addition, this functionality as described will be useful for parallel script loaders, giving them the ability to download multiple scripts in parallel, but control their execution order by deferring the execution of all of them, and then once they are all loaded, executing them on-demand in desired order. The base-version of this application of the use-case was addressed by the &amp;quot;async=false&amp;quot; behavior, recently adopted by the Specification, but would be bolstered and improved by the functionality in these current proposals. &lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
There&#039;s no way to defer the execution of a script element, except by use of the `defer` attribute in markup script elements (meaning it&#039;s inaccessible to dynamic script loaders). However, the `defer` attribute in markup still only allows deferring until after DOMContentLoaded (aka, &amp;quot;DOM-ready&amp;quot;). Several applications of the use-case call for an abitrary, author-controlled point to decide when scripts should execute, not locked to DOM-ready.&lt;br /&gt;
&lt;br /&gt;
With regards to the &amp;quot;async=false&amp;quot; behavior being improved with this proposal, script loaders cannot currently (with &amp;quot;async=false&amp;quot;) specify multiple &amp;quot;groups&amp;quot; of scripts which have order-dependency within the group, but the groups themselves are completely independent. There&#039;s no way to currently download all scripts in groups 1 and 2, but allow either group 1 or group 2 to execute when the first one finishes. Currently, the only way to do this based on specification is to use &amp;quot;async=false&amp;quot;, and then all the scripts will execute in the order added to the DOM -- the &amp;quot;independent grouping&amp;quot; of execution is not possible.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
Currently, this functionality is available in IE (since version 4). To achieve this functionality in other browsers, there&#039;s two main types of hacks to accomplish it:&lt;br /&gt;
&lt;br /&gt;
1. The content may be &amp;quot;wrapped&amp;quot;, for instance in /* ... */ comments, such that its automatic execution will be moot. Then, the page can, at the desired time, grab the script content, remove the comment markers, and re-execute the code. This technique only works for inline script elements, not for externally loaded script elements (which also means that any caching benefit is lost).&lt;br /&gt;
&lt;br /&gt;
2. The page can employ one of a variety of &amp;quot;cache preload&amp;quot; tricks (different for each browser), whereby the script is loaded into the cache but not executed, and then when execution is desired, the script is re-requested with a proper script element, and thus executed. The main drawback to this approach is the assumption of cacheability. If a script was not properly cached, this technique will result in a costly double-load. Also, as stated, these hacks are brittle and require different variations for each browser. They are quite susceptible to future restrictions by the Specification, or by changes to browsers&#039; optimization techniques.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The primary benefit underlying all the use-cases of this proposal is performance. It gives the page more control over execution of downloaded scripts, allowing the page author to make tradeoffs on when scripts are executed in an attempt to optimize for the user-conditions and experience.&lt;br /&gt;
&lt;br /&gt;
The secondary benefit is to reduce (greatly) the complexity (and hackiness) of script loaders (like LABjs, ControlJS, etc) which go to great lengths to employ tricks and hacks to accomplish this described functionality in existing browsers.&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.whatwg.org/pipermail/whatwg-whatwg.org/2011-February/030161.html &amp;quot;Proposal for separating script downloads and execution&amp;quot; (WHATWG email list thread)]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[[Dynamic_Script_Execution_Order]]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== Proposal 1 (Nicholas Zakas) ===&lt;br /&gt;
This proposal is described in detail in Nicholas Zakas&#039; [https://docs0.google.com/document/d/1EGM9xmQXbJ_rI0IFhbnACiDaaBPTSb7T3RynwD-naJg/edit?hl=en&amp;amp;authkey=CO7aqZAO Delayed Script Execution v2.1], but will be summarized here for clarity of this page.&lt;br /&gt;
&lt;br /&gt;
This proposal recognizes the need for an event mechanism to detect when a script is preloaded, as well as the need for a feature-detect to detect if the functionality is implemented in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Add a `preload` property to dynamic script-created script elements (*not* markup script elements), which defaults to false, that can be set to `true` *before* the setting of the `src` property. Turning on &amp;quot;preload&amp;quot; will cause the script to be loaded immediately, even if the script has not yet been added to the DOM.&lt;br /&gt;
&lt;br /&gt;
2. Add a `onpreload` event, which fires when the script element finishes preloading. The script element can then, at that event time, or later, be executed by adding the script element to the DOM through normal procedures.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (typeof document.createElement(“script”).preload == “boolean”)&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to be as semantic as possible to what is actually occurring (thus the use of the very descriptive and appropriately named `preload` and `onpreload`).&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. This proposal&#039;s main limitation is that it requires a new property and a new event (which is not precedented anywhere else in the HTML specification yet). That means that the proposal will require more effort on the part of the Specification as well as on the part of browsers to implement.&lt;br /&gt;
&lt;br /&gt;
2. In fact, for full adherence to this proposal, it will actually require a change to IE&#039;s existing behavior, to make it not &amp;quot;preload&amp;quot; if the `preload` attribute is not set. This is quite possible to create backwards-compat concerns from the IE team.&lt;br /&gt;
&lt;br /&gt;
=== Proposal 2 (Kyle Simpson) === &lt;br /&gt;
This proposal is, in spirit, very similar to Proposal 1, but is a little bit simpler (in that it doesn&#039;t add a new property or unprecedented event mechanism). Whereas Proposal 1 seeks to add a `preload` attribute to the script element to signal that it should have the preloading (load-but-not-execute) behavior, this proposal builds on automatic behavior for preloading already suggested by the current HTML Specification:&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Put simply, this wording says that a script may begin downloading as soon as the `src` property is set on a dynamic script element, even if that element is not added to the DOM. Further, it suggests that adding the element to the DOM is necessary to execute the script. From this, we can infer that a script may be &amp;quot;preloaded&amp;quot; by creating and keeping references to script elements, but not adding them to the DOM. And then when the script is desired to execute, the script element can be added to the DOM.&lt;br /&gt;
&lt;br /&gt;
In addition to this wording already being in the specification, the functionality as described has been implemented in IE since version 4.&lt;br /&gt;
&lt;br /&gt;
As with the [[Script_Execution_Order_Control#Proposal_1_.28Nicholas_Zakas.29|proposal from Nicholas Zakas]], this proposal also requires an event mechanism which can allow the web author to be notified of when a script element is finished preloading. In addition, both proposals require a &amp;quot;feature-detect&amp;quot; so that web authors can opt-in to the described behavior after detecting that it is present in a browser.&lt;br /&gt;
&lt;br /&gt;
The proposal is:&lt;br /&gt;
&lt;br /&gt;
1. Change the above referenced wording (as necessary) to indicate that the preloading behavior is required rather than being a performance suggestion.&lt;br /&gt;
&lt;br /&gt;
2. Taking IE&#039;s implementation as the pattern, specify the `readyState` property on the script element (defaults to &amp;quot;uninitializd&amp;quot;), and fire `onreadystatechange` events when the script is &amp;quot;loaded&amp;quot; (finished preloading) and &amp;quot;complete&amp;quot; (finished executing, ~ similar to `onload`). The `readyState` functionality is already precedented in the specification for the XHR object, so that same pattern and wording could/should be applied here.&lt;br /&gt;
&lt;br /&gt;
3. The feature-detect will be:&lt;br /&gt;
&lt;br /&gt;
   (document.createElement(&amp;quot;script&amp;quot;).readyState == &amp;quot;uninitialized&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
The reason for checking not only the `readyState` variable but also its value is that Opera currently implements a non-functional `readyState` property on the script element (but does NOT support the proposed functionality), but Opera&#039;s default value is &amp;quot;complete&amp;quot;. The feature-test would give a false-positive in Opera if the value isn&#039;t included in the test. Opera has stated in the WHATWG discussion thread that they would not not change the default value of `readyState` to &amp;quot;uninitialized&amp;quot; unless they were also implementing the behavior described in this proposal. No other known browsers have `readyState` on script elements, and the assumption is that any new browser implementations adopting it would adhere to the specification (if this proposal is adopted). So, the proposed feature-detect should be reliable.&lt;br /&gt;
&lt;br /&gt;
This proposal allows scripts to be added to the DOM before they are finished loading, as they currently can, with no change in current behavior. It also does not specify any different model for script execution than is currently present in browsers. In other words, under the circumstances that a browser would execute a script synchronously, the same would be true, and under the circumstances a script would be queued for execution asynchronously as soon as possible, the same would also be true.&lt;br /&gt;
&lt;br /&gt;
The goal of this proposal is to make the minimum change to both specification and browser behavior necessary to fulfill all the goals of the stated use-case. It does not introduce any new, unprecedented elements or concepts, and instead builds on existing specification wording and actual browser implementation.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
1. The feature-detect is riskier/weaker/more brittle, and not as graceful as in Proposal 1&lt;br /&gt;
&lt;br /&gt;
* Yes, it&#039;s less graceful of a feature-detect. But as explained, it *is* functional. Given appropriate evangelism and involvement with browser vendors, if this proposal were implemented as described, the feature-detect should not be risky.&lt;br /&gt;
&lt;br /&gt;
2. The proposal does not address the use-case of deferring inline script elements&#039; execution&lt;br /&gt;
&lt;br /&gt;
* Yes, neither of the proposals are seeking to fill this use-case, but this proposal is not really capable of it, whereas the other proposal *could* be extended to include markup script element support for that use-case. There&#039;s not yet been sufficient evidence of that use-case being necessary.&lt;br /&gt;
&lt;br /&gt;
== Insufficient Solution Suggestions ==&lt;br /&gt;
TODO: fill out this section with the other suggestions that have been discussed but deemed insufficient for the use-case.&lt;br /&gt;
&lt;br /&gt;
[[Category:Proposals]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Talk:Dynamic_Script_Execution_Order&amp;diff=5677</id>
		<title>Talk:Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Talk:Dynamic_Script_Execution_Order&amp;diff=5677"/>
		<updated>2010-12-24T00:50:45Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* response */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please discuss the proposal here. All informal feedback is welcomed, and when it&#039;s appropriate, please add content to the main page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== What about CommonJS / RequireJS? ==&lt;br /&gt;
&lt;br /&gt;
I&#039;m not sure we need to complicate the HTML or browser engines in order to satisfy the advanced use cases.  To me it seems that the JavaScript langauge itself could abstract the requirements and let the browser vendors implement whatever solution they see fit.  Specifically, if JavaScript provided the define() and require() calls proposed in the CommonJS &amp;quot;specifications&amp;quot; (and implemented by [http://requirejs.org/docs/api.html RequireJS], [http://svn.dojotoolkit.org/src/dojo/trunk/_base/_loader/loader.js dojo (trunk)], and others *Yabble?), then browsers could just load and evaluate scripts as fast as possible without regard to script execution order.  &lt;br /&gt;
&lt;br /&gt;
This would solve all of the problems except for the current inability to use loaders like LABjs in FF4 or webkit.  To solve that problem, we should consider keeping the existing functionality or triggering the deprecated functionality when needed.&lt;br /&gt;
&lt;br /&gt;
--[[User:Unscriptable|Unscriptable]] 01:29, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Response ===&lt;br /&gt;
&lt;br /&gt;
This is a valid point except that it assumes that all code that needs to be loaded is &amp;quot;modular&amp;quot;... in fact, most code on the internet that needs to be loaded is NOT modular, and instead requires particular ordering when being loaded... for instance, &amp;quot;jquery&amp;quot; and &amp;quot;jquery-ui&amp;quot; -- jQuery must be run before jQuery-UI is loaded and run.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The spirit of this proposal is to add the facility to native browser (rendering engine, specifically) behavior, such that any and all existing content is able to be loaded (even with dependencies and order) without modification.&lt;br /&gt;
&amp;lt;blockquote style=background-color:#eee&amp;gt;&lt;br /&gt;
Are we trying to codify how one would implement this HTML dynamically so I can achieve parallel loading but retain dependency that is inferred in the order? &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: white&amp;quot;&amp;gt; &lt;br /&gt;
&amp;amp;lt;script src=&amp;quot;jquery.js&amp;quot;&amp;amp;gt; &amp;amp;lt;/script&amp;amp;gt; &amp;lt;br/&amp;gt; &lt;br /&gt;
&amp;amp;lt;script src=&amp;quot;jquery-ui.js&amp;quot;&amp;amp;gt; &amp;amp;lt;/script&amp;amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If so, we must consider the case in which the dependency itself loads additional code which the discussions around `async=false` or even text/cache do not account for.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:23, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dependent code should be modular ===&lt;br /&gt;
&lt;br /&gt;
I completely agree, 99% of JavaScript in use today is NOT modular, but that&#039;s arguably because there was no built-in support to make it modular (IMHO, YMMV, RTFM, BBQ, ROFL).  But that doesn&#039;t mean there aren&#039;t dependencies that span script files / script nodes.  I don&#039;t know how many sites would be broken (or if there is any way to determine how many sites would be broken) if/when the latest browsers started loading and evaluating script-injected scripts out of order.  It&#039;s not just LABjs-based sites.  I&#039;ll bet many dojo and YUI sites will break, too (although I don&#039;t know this for a fact because I don&#039;t know exactly how their dynamic script loaders work).  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;In any case, breaking existing sites is a BAD IDEA. :)  There should be some way to allow LABjs and other dynamic script loaders to continue to work.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The part I don&#039;t agree with in the proposals is that it should be the job of HTML and/or the browser engine to manage dependencies.  Specifically, I am talking about the scriptGroup element and the &amp;quot;waitFor&amp;quot; attribute.  The &amp;quot;async&amp;quot; attribute may be the right level: it appropriates only general heuristics to the browser: &#039;&#039;load these scripts whenever you like, but load these other scripts in this order&#039;&#039;.  &lt;br /&gt;
&lt;br /&gt;
Detailed dependency management belongs in the modules, though.  This is how it&#039;s done in most environments / languages: import statements, include statements, etc.  It&#039;s no different in JavaScript, if a developer wants to take advantage of some code in another module, she/he should import it into their current module.  This should be the model moving forward.  It&#039;s not hard, it&#039;s not un-web-like, and it&#039;s already being done.  &lt;br /&gt;
&lt;br /&gt;
Existing code can be made to work modularly, too:&lt;br /&gt;
&lt;br /&gt;
1. Identify modules and wrap them with a define() method.&lt;br /&gt;
2. Identify dependencies using either the [http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition Asynchronous Module Definition] format or the CommonJS synchronous format&lt;br /&gt;
&lt;br /&gt;
async format:&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
define([&#039;dependency1&#039;, &#039;dependency2&#039;], function (dependency1, dependency2) {&lt;br /&gt;
	/* this is the factory function, return the module inside here */&lt;br /&gt;
	return { /* some object, but could also be a constructor or a function */ };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
sync format:&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
define(function (require, exports, module) {&lt;br /&gt;
	/* this is the factory function, require dependencies and return the module inside here */&lt;br /&gt;
	var dependency1 = require(&#039;dependency1&#039;);&lt;br /&gt;
	var dependency2 = require(&#039;dependency2&#039;);&lt;br /&gt;
	return { /* some object, but could also be a constructor or a function */ };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the async case, the browser may employ whatever method it sees fit (e.g. parallel) to download the dependencies since the code inside the factory function won&#039;t execute until all of the dependencies are loaded and evaluated.  In the sync case, the browser has no choice but to download and evaluate the dependency before continuing code execution.  It&#039;s conceivable that the browser could find and asynchronously pre-fetch the inline require()&#039;ed dependencies in the sync case.  (RequireJS doesn&#039;t quite do this.  Instead I believe it finds the inline require() calls and converts the module to an async module.)  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;So, in short: don&#039;t break LABjs (or any other script loaders), but don&#039;t extend the spec to manage dependency management from HTML.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
--[[User:Unscriptable|Unscriptable]] 01:29, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Response #2 ==&lt;br /&gt;
&lt;br /&gt;
The cat&#039;s already out of the bag... we&#039;ve got a dozen+ years worth, hundreds of millions of javascript files out there, which are NOT modular, and will not be modified to be modular. &lt;br /&gt;
&lt;br /&gt;
If we&#039;re talking about either trying to change all the JS on the web to be modular, or trying to extend the HTML markup spec to include a better facility for accomodating the existing script content, I think the former is idealistic and &amp;quot;correct&amp;quot; but the latter is realistic and &amp;quot;right&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
A modular loading system will only work for modular code, but a general loading mechanism in HTML (like the &amp;amp;lt;script&amp;gt; tag) can load both modular and non-modular code, which is why that more general mechanism is what&#039;s in HTML right now and not some more specialized loading API that only loads a small percentage of JavaScript code out there.&lt;br /&gt;
&lt;br /&gt;
The &amp;amp;lt;script&amp;gt; tag has worked fine forever, and there&#039;s been no real big push to do away with it in favor of having all loading handled by JavaScript -- in fact, that would be impossible because if you had no way to load JS onto the page with a &amp;amp;lt;script&amp;gt; tag, then no JavaScript could run on a page to load more code. So I don&#039;t agree that we should stop adding functionality to the HTML loading mechanism and only focus on JavaScript.&lt;br /&gt;
&lt;br /&gt;
If we agree that the &amp;amp;lt;script&amp;gt; tag itself is not only required but normative, then I&#039;m not sure why decorating the &amp;amp;lt;script&amp;gt; tag with additional behavior to help it be more powerful is a bad pattern. We&#039;ve addded &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; already. I&#039;m not sure I understand why adding a wrapper tag called &amp;quot;scriptGroup&amp;quot; would be that much different than attributes on the &amp;amp;lt;script&amp;gt; tag?&lt;br /&gt;
&lt;br /&gt;
I also don&#039;t think that &amp;quot;scriptGroup&amp;quot; constitutes &amp;quot;dependency management&amp;quot;. &amp;quot;scriptGroup&amp;quot; is a semantic way for a web author to express that they want to group a set of scripts together ONLY so far as execution order behavior is concerned. It implies nothing more to it (that &amp;quot;dependency management&amp;quot; would imply) than that.&lt;br /&gt;
&lt;br /&gt;
For instance, consider this: if JavaScript had been built with non-global context and statement level context and sandboxing and such things, we&#039;d have a much more secure JavaScript right now. But just because JavaScript may someday be taught some new tricks in terms of security, that doesn&#039;t mean that &amp;quot;now&amp;quot; HTML shouldn&#039;t try to help solve the problem. If for instance the &amp;amp;lt;frag&amp;gt; tag can be used to create a sandboxed environment for third-party JavaScript code to run in, this is a much easier way to address the security problem than to suggest we have to wait for ES-Harmony to add extra syntax to the language to accomplish the same thing.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 02:13, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Text/cache solution ==&lt;br /&gt;
&lt;br /&gt;
It seems to me that having a real text/cache script type, or a similar element that fires a load/error event and provides a .text property gets you where you need to be. You could setAttribute(&amp;quot;type&amp;quot;, &amp;quot;text/JavaScript&amp;quot;) or use the text to create a new script element. Or a new style element, or whatever. That puts you in control of when script or CSS is interpreted. With async as the default, you don&#039;t need a new ordered attribute as that&#039;s easily managed in script.&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;text/cache&amp;quot; response ==&lt;br /&gt;
&lt;br /&gt;
On the surface, this idea sounds like it&#039;s pretty similar to &amp;amp;lt;link rel=prefetch&amp;gt; -- in other words, we&#039;ve already standardized a way to prefetch a resource into cache, and probably in a better way than the hack that I did in LABjs of just relying on it fetching unrecognizable content.&lt;br /&gt;
&lt;br /&gt;
Unfortunately, it still suffers some similar problems to &amp;amp;lt;link rel=prefetch&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Firstly, if the notion is that you&#039;d use &amp;amp;lt;script type=&amp;quot;text/cache&amp;quot;&amp;gt; to prefetch, and then you&#039;d make a second request for the resource with a real &amp;amp;lt;script type=&amp;quot;text/javascript&amp;gt; node, it suffers the faulty assumption that the resource was served with proper cache headers.&lt;br /&gt;
&lt;br /&gt;
If we try to avoid the need for the second request by suggesting that you can access the `text` property of the node (and then eval() or inject that code when you want to execute it), this runs afoul of the same-origin policy problem if you requested the script from a non-local domain.&lt;br /&gt;
&lt;br /&gt;
The part of your suggestion that holds water would be if, given a &amp;quot;prefetched&amp;quot; script node that didn&#039;t execute yet, I could simply change that node&#039;s `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;, and that would explicitly trigger the browser to immediately execute it. &lt;br /&gt;
&lt;br /&gt;
This is an interesting idea. I can see though that it perhaps has some potential complications. &lt;br /&gt;
&lt;br /&gt;
For instance, would the browser have to keep a &amp;quot;hasExecuted&amp;quot; internal flag, so that changing the `type` value would only at most result in one execution? Also, if the script node in question is loaded but not executed, can the page alter the script code (by setting its `text` property) before making the change to the `type` value? In that case, should the browser execute the original code or the modified code? I can see security implications and confusion abounding in that behavior.&lt;br /&gt;
&lt;br /&gt;
Lastly, as mentioned above, this basically copies (in a similar but not exactly identical way) the &amp;amp;lt;link rel=prefetch&amp;gt; behavior. It may be seen as a negative to be trying to step on the toes of &amp;amp;lt;link rel=prefetch&amp;gt;, and also it may be more confusing to the general public as to why you&#039;d use one technique or the other, given their differences being so nuanced.&lt;br /&gt;
&lt;br /&gt;
That having been said, I can see that it&#039;s a viable alternate proposal. It&#039;s worth adding a note to that section of the main Wiki page and to getting some feedback from browser vendors on if they feel this is a more realistic/suitable solution to the use-case.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #eee&amp;quot;&amp;gt;&lt;br /&gt;
The difference between pre-fetching and this proposal seems pretty clear: Prefetching, if not performed now, will eventually be done once the user explicitly requests the page. while the &amp;quot;text/cache&amp;quot; solution indicates that the resource should load &#039;&#039;&#039;immediately&#039;&#039;&#039;, not during idle time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:10, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 20:56, 4 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== RequireJS order support ==&lt;br /&gt;
&lt;br /&gt;
I am the main developer for RequireJS, and just want to voice my support for a solution that works on the script element itself, either by leveraging the async attribute or allowing something like an &amp;quot;order&amp;quot; boolean attribute that would allow specifying a dynamically added script element should be executed in the order that it appears in the DOM. This capability should be something can be capability detected to avoid browser sniffing.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
Why shouldn&#039;t dependency management be implemented in high-level code and the browser simply provide a means by which parallel loading can be achieved?  As you know, there is a considerable amount of nuance.  I&#039;m of the opinion that implementing dependency management in the engine will eventually lead us right back to where we were 10 years ago with inline script tags which provides an easy method for dependency management at the expense of unmanaged blocking and an inability to detect or adapt to errors. &amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:51, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
There are many scripts on the web today that have implicit dependencies that should be loaded in a specific order, but the application developer will want to use dynamically created script elements for best performance. jQuery plugins that depend on the jQuery script already been executed in the page are a good example.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
Agreed, but I don&#039;t believe the proposals allow for proper emulation of the dependency management provided by script tags.  Prototype.js, for example, loads its core modules dynamically (using document.write so maybe not the best example...).  When prototype.js is included via script tags, ensuing script blocks are free to &amp;quot;depend&amp;quot; on all of these modules; Dynamically creating a dependency on prototype.js by loading both with the `async=true` property will fail since prototype.js won&#039;t be able to &amp;quot;jump the queue&amp;quot; and add insure its core modules load before its extensions.  &amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:51, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
I have to admit, you raise a valid point with respect to that common usage. The YUI model (where the framework is responsible for all loading) is perhaps the more appropriate model to move toward, though. Or, the framework doesn&#039;t auto-load its own dependencies at all. In either case, I think it&#039;s cleaner to have one loading mechanism in play rather than two or more competing ones, because as you point out, this is going to get messy. I still am not sure this means that the async=false proposal is lacking, because as I think about how complicated such a split loading would be with your `readyState` proposal, that seems even worse/more complicated to me. [[User:Getify|Getify]] 20:06, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
I would not argue &#039;&#039;&#039;against&#039;&#039;&#039; a global queue for synchronized execution, I just think the usefulness of such a construct declines as scripts that &amp;quot;I don&#039;t care about&amp;quot; are added.  It&#039;s my opinion the spec should provide the ability for high-level code to implement similar constructs, regardless of how complex it may be to do so. &amp;lt;br/&amp;gt; &lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
That being said, I don&#039;t think it&#039;s any more complicated than existing models.  Here&#039;s a demonstration of an [http://digital-fulcrum.com/executionorder/ IE-only parallel-loading, synchronously-executing script queue] implemented in 40 lines of code.  It tests both cached and un-cached scripts (from the LABjs test suite) and has been tested and found to be working in IE6-8. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[User:Serverherder|Serverherder]] 01:52, 16 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
- James Burke&lt;br /&gt;
&lt;br /&gt;
== Vendors Should Implement the Existing Suggestion ==&lt;br /&gt;
&lt;br /&gt;
Since the spec all ready includes a suggestion to download resources when the SRC attribute is set, parallel loading is an option the &#039;&#039;&#039;vendor&#039;&#039;&#039; chooses to implement and not something a script loader should be able to require.  Vendors that don&#039;t implement the suggestion will run the risk of being less performant in these situations.  That, however, is &#039;&#039;&#039;their&#039;&#039;&#039; choice to make as the spec suggests.  If the benefits of parallel loading dependencies are so great, users will opt for this in their choice of agent or vendors can adopt the existing mechanism.&lt;br /&gt;
&lt;br /&gt;
== The Current Suggestion Should be Changed to a Mandate ==&lt;br /&gt;
&lt;br /&gt;
The spec is pretty clear on why it chose to make this optional.  If dependent script loading is so common that browsers that adopt the suggestion enjoy considerably better performance, the user will eventually decide to opt for those implementations.  On the other hand, if it is more common that scripts are created and never inserted, leading to wasted bandwidth and clogged fetching queues, the converse will occur.  Either way, by making it optional, the spec hedges its bet and is more prone to be future-proof as the habits of script developers evolve.&lt;br /&gt;
&lt;br /&gt;
== Modify the specification so parallel-loading is configurable  ==&lt;br /&gt;
The specification expresses the reasons parallel loading should not be done.  To this point, allowing developers a means to &#039;&#039;&#039;require&#039;&#039;&#039; resources be downloaded in parallel is in direct opposition to the spec&#039;s all ready stated position.  To this end, I see no purpose for async=true: A browser that hopes to be highly performant should implement the spec&#039;s suggestion to improve performance.  Developers should not be able to circumvent the vendor&#039;s attempt to be conservative in its use of bandwidth simply to achieve ordered execution.&lt;br /&gt;
&lt;br /&gt;
=== response ===&lt;br /&gt;
Nothing about this proposal requires any parallelism that the browser wouldn&#039;t already afford. The spirit of this proposal is not about loading behavior, but about execution order.&lt;br /&gt;
&lt;br /&gt;
The spec says nothing about the parallel loading, nor do I think it should necessarily. All modern browsers HAVE in fact implemented parallel loading for all scripts, regardless of async or not. And I think this is a good thing. But it has nothing to do with `async` or my proposal to make the definition of `async` symmetric/consistent between markup script elements and dynamic script elements.&lt;br /&gt;
&lt;br /&gt;
The spec already defines that `async` is specifically to create unblocking of the page&#039;s resource loading AND &amp;quot;as soon as possible&amp;quot; execution of the script. But it only defines this for parser-inserted (markup) script tags. I think this is short-sighted, because I think a script-loader should be able to opt into either ordered or unordered execution in the same way that markup script elements can. That&#039;s really all the main proposal is about.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#EEE&amp;quot;&amp;gt;&lt;br /&gt;
Script loaders can all ready choose ordered execution: chain the onload events.  The issue is that vendors have chosen &#039;&#039;&#039;not&#039;&#039;&#039; to adopt the specification&#039;s suggestion. The high priority vendors place on performance suggests this was not done consciously.  Instead of advocating WHATWG for additions to the spec, I think we may be better off advocating vendors implement the spec&#039;s suggestion.  The existing implementation of parallelism for parser-inserted scripts suggests vendors would be willing to do so and the existing lack of support is merely an issue of communication.  &lt;br /&gt;
&lt;br /&gt;
At its core, the existing suggestion to retrieve upon src assignment is not altogether different than the eager-fetching performed for parser-inserted scripts.  The risk still exists that the browser download a script that is never actually executed.  For example, below a bunch of script tags, the HTML may contain a closing comment tag (--&amp;gt;) and one of the scripts doc.write(&amp;quot;&amp;lt;\!--&amp;quot;).  Another risk is that the eager-fetching clog the fetch queue, delaying the download of any scripts included via document.write().    Vendors have &#039;&#039;&#039;chosen&#039;&#039;&#039; to assume this risk though, since its probability is low and the benefit high.  &lt;br /&gt;
&lt;br /&gt;
Likewise, vendors can calculate the risk/reward for script-inserted scripts and will likely make the same decision -- they implement this exact behavior for images.  Script loaders should respect the fact that a vendor has consciously chosen &#039;&#039;&#039;not&#039;&#039;&#039; to support parallelism for script-inserted scripts.  Providing a means by which this decision can be circumvented undermines their choice.   I believe it was in that spirit the specification leaves eager fetching script resources as a suggestion.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
I think you still miss the pragmatic point here. I&#039;m not disagreeing with you in theory, but in practice, the hard-nosed approach you suggest would spell death for any script loader. That&#039;s not a chance I&#039;m willing to take.&lt;br /&gt;
&lt;br /&gt;
When a developer uses a script loader that claims it is &amp;quot;The Performance Script Loader&amp;quot; (as LABjs does), and they test in some latest-version browser (whether that be FF, Safari, Chrome, IE, or Opera) and the performance is terrible (worse than before they installed LABjs), they&#039;re not going to say, &amp;quot;Well, I&#039;ll just keep LABjs in there and hope those browsers fix this quickly&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
This is tail-wagging-the-dog mentality, and it ignores the reality: people will almost always chose the path of least resistance. If they try a script loader, and it gives them worse performance than before, they&#039;ll simply take it out. They may complain about it once in a tweet, but they won&#039;t take a stand against the browsers to the detriment of their own site and their own users, and they won&#039;t tell all their users to switch to another browser in protest.&lt;br /&gt;
&lt;br /&gt;
And worse, for LABjs, the path of least resistance may very well mean that if performance is important to them, they&#039;ll just pick some other loader, which has more hacks in it. As long as they get what they want, which is reliable and pretty good performance, they&#039;ll be happy. And it will totally have been lost on them that the battle for web standards was just dealt a blow by their actions. General web page authors do not care to fight that battle on behalf of LABjs. So, LABjs must fight that battle under the covers.&lt;br /&gt;
&lt;br /&gt;
I understand your perspective, but I can say as the maintainer of a tool like LABjs that cares about keeping its relevancy for &amp;quot;survival&amp;quot;, I won&#039;t take an action that I know is likely to cause a decline in the usefulness of my tool. Instead, I will take a more cautious, slow-to-change approach, while at the same time feature-testing for the new standards and using them if available. Only after a long time of the new feature-testable stuff being present will I consider removing the legacy stuff. This is a strategy I think almost every framework/lib maintainer would echo support for.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 00:50, 24 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 21:39, 23 December 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Talk:Dynamic_Script_Execution_Order&amp;diff=5676</id>
		<title>Talk:Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Talk:Dynamic_Script_Execution_Order&amp;diff=5676"/>
		<updated>2010-12-24T00:50:04Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* response */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please discuss the proposal here. All informal feedback is welcomed, and when it&#039;s appropriate, please add content to the main page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== What about CommonJS / RequireJS? ==&lt;br /&gt;
&lt;br /&gt;
I&#039;m not sure we need to complicate the HTML or browser engines in order to satisfy the advanced use cases.  To me it seems that the JavaScript langauge itself could abstract the requirements and let the browser vendors implement whatever solution they see fit.  Specifically, if JavaScript provided the define() and require() calls proposed in the CommonJS &amp;quot;specifications&amp;quot; (and implemented by [http://requirejs.org/docs/api.html RequireJS], [http://svn.dojotoolkit.org/src/dojo/trunk/_base/_loader/loader.js dojo (trunk)], and others *Yabble?), then browsers could just load and evaluate scripts as fast as possible without regard to script execution order.  &lt;br /&gt;
&lt;br /&gt;
This would solve all of the problems except for the current inability to use loaders like LABjs in FF4 or webkit.  To solve that problem, we should consider keeping the existing functionality or triggering the deprecated functionality when needed.&lt;br /&gt;
&lt;br /&gt;
--[[User:Unscriptable|Unscriptable]] 01:29, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Response ===&lt;br /&gt;
&lt;br /&gt;
This is a valid point except that it assumes that all code that needs to be loaded is &amp;quot;modular&amp;quot;... in fact, most code on the internet that needs to be loaded is NOT modular, and instead requires particular ordering when being loaded... for instance, &amp;quot;jquery&amp;quot; and &amp;quot;jquery-ui&amp;quot; -- jQuery must be run before jQuery-UI is loaded and run.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The spirit of this proposal is to add the facility to native browser (rendering engine, specifically) behavior, such that any and all existing content is able to be loaded (even with dependencies and order) without modification.&lt;br /&gt;
&amp;lt;blockquote style=background-color:#eee&amp;gt;&lt;br /&gt;
Are we trying to codify how one would implement this HTML dynamically so I can achieve parallel loading but retain dependency that is inferred in the order? &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: white&amp;quot;&amp;gt; &lt;br /&gt;
&amp;amp;lt;script src=&amp;quot;jquery.js&amp;quot;&amp;amp;gt; &amp;amp;lt;/script&amp;amp;gt; &amp;lt;br/&amp;gt; &lt;br /&gt;
&amp;amp;lt;script src=&amp;quot;jquery-ui.js&amp;quot;&amp;amp;gt; &amp;amp;lt;/script&amp;amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If so, we must consider the case in which the dependency itself loads additional code which the discussions around `async=false` or even text/cache do not account for.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:23, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dependent code should be modular ===&lt;br /&gt;
&lt;br /&gt;
I completely agree, 99% of JavaScript in use today is NOT modular, but that&#039;s arguably because there was no built-in support to make it modular (IMHO, YMMV, RTFM, BBQ, ROFL).  But that doesn&#039;t mean there aren&#039;t dependencies that span script files / script nodes.  I don&#039;t know how many sites would be broken (or if there is any way to determine how many sites would be broken) if/when the latest browsers started loading and evaluating script-injected scripts out of order.  It&#039;s not just LABjs-based sites.  I&#039;ll bet many dojo and YUI sites will break, too (although I don&#039;t know this for a fact because I don&#039;t know exactly how their dynamic script loaders work).  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;In any case, breaking existing sites is a BAD IDEA. :)  There should be some way to allow LABjs and other dynamic script loaders to continue to work.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The part I don&#039;t agree with in the proposals is that it should be the job of HTML and/or the browser engine to manage dependencies.  Specifically, I am talking about the scriptGroup element and the &amp;quot;waitFor&amp;quot; attribute.  The &amp;quot;async&amp;quot; attribute may be the right level: it appropriates only general heuristics to the browser: &#039;&#039;load these scripts whenever you like, but load these other scripts in this order&#039;&#039;.  &lt;br /&gt;
&lt;br /&gt;
Detailed dependency management belongs in the modules, though.  This is how it&#039;s done in most environments / languages: import statements, include statements, etc.  It&#039;s no different in JavaScript, if a developer wants to take advantage of some code in another module, she/he should import it into their current module.  This should be the model moving forward.  It&#039;s not hard, it&#039;s not un-web-like, and it&#039;s already being done.  &lt;br /&gt;
&lt;br /&gt;
Existing code can be made to work modularly, too:&lt;br /&gt;
&lt;br /&gt;
1. Identify modules and wrap them with a define() method.&lt;br /&gt;
2. Identify dependencies using either the [http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition Asynchronous Module Definition] format or the CommonJS synchronous format&lt;br /&gt;
&lt;br /&gt;
async format:&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
define([&#039;dependency1&#039;, &#039;dependency2&#039;], function (dependency1, dependency2) {&lt;br /&gt;
	/* this is the factory function, return the module inside here */&lt;br /&gt;
	return { /* some object, but could also be a constructor or a function */ };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
sync format:&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
define(function (require, exports, module) {&lt;br /&gt;
	/* this is the factory function, require dependencies and return the module inside here */&lt;br /&gt;
	var dependency1 = require(&#039;dependency1&#039;);&lt;br /&gt;
	var dependency2 = require(&#039;dependency2&#039;);&lt;br /&gt;
	return { /* some object, but could also be a constructor or a function */ };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the async case, the browser may employ whatever method it sees fit (e.g. parallel) to download the dependencies since the code inside the factory function won&#039;t execute until all of the dependencies are loaded and evaluated.  In the sync case, the browser has no choice but to download and evaluate the dependency before continuing code execution.  It&#039;s conceivable that the browser could find and asynchronously pre-fetch the inline require()&#039;ed dependencies in the sync case.  (RequireJS doesn&#039;t quite do this.  Instead I believe it finds the inline require() calls and converts the module to an async module.)  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;So, in short: don&#039;t break LABjs (or any other script loaders), but don&#039;t extend the spec to manage dependency management from HTML.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
--[[User:Unscriptable|Unscriptable]] 01:29, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Response #2 ==&lt;br /&gt;
&lt;br /&gt;
The cat&#039;s already out of the bag... we&#039;ve got a dozen+ years worth, hundreds of millions of javascript files out there, which are NOT modular, and will not be modified to be modular. &lt;br /&gt;
&lt;br /&gt;
If we&#039;re talking about either trying to change all the JS on the web to be modular, or trying to extend the HTML markup spec to include a better facility for accomodating the existing script content, I think the former is idealistic and &amp;quot;correct&amp;quot; but the latter is realistic and &amp;quot;right&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
A modular loading system will only work for modular code, but a general loading mechanism in HTML (like the &amp;amp;lt;script&amp;gt; tag) can load both modular and non-modular code, which is why that more general mechanism is what&#039;s in HTML right now and not some more specialized loading API that only loads a small percentage of JavaScript code out there.&lt;br /&gt;
&lt;br /&gt;
The &amp;amp;lt;script&amp;gt; tag has worked fine forever, and there&#039;s been no real big push to do away with it in favor of having all loading handled by JavaScript -- in fact, that would be impossible because if you had no way to load JS onto the page with a &amp;amp;lt;script&amp;gt; tag, then no JavaScript could run on a page to load more code. So I don&#039;t agree that we should stop adding functionality to the HTML loading mechanism and only focus on JavaScript.&lt;br /&gt;
&lt;br /&gt;
If we agree that the &amp;amp;lt;script&amp;gt; tag itself is not only required but normative, then I&#039;m not sure why decorating the &amp;amp;lt;script&amp;gt; tag with additional behavior to help it be more powerful is a bad pattern. We&#039;ve addded &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; already. I&#039;m not sure I understand why adding a wrapper tag called &amp;quot;scriptGroup&amp;quot; would be that much different than attributes on the &amp;amp;lt;script&amp;gt; tag?&lt;br /&gt;
&lt;br /&gt;
I also don&#039;t think that &amp;quot;scriptGroup&amp;quot; constitutes &amp;quot;dependency management&amp;quot;. &amp;quot;scriptGroup&amp;quot; is a semantic way for a web author to express that they want to group a set of scripts together ONLY so far as execution order behavior is concerned. It implies nothing more to it (that &amp;quot;dependency management&amp;quot; would imply) than that.&lt;br /&gt;
&lt;br /&gt;
For instance, consider this: if JavaScript had been built with non-global context and statement level context and sandboxing and such things, we&#039;d have a much more secure JavaScript right now. But just because JavaScript may someday be taught some new tricks in terms of security, that doesn&#039;t mean that &amp;quot;now&amp;quot; HTML shouldn&#039;t try to help solve the problem. If for instance the &amp;amp;lt;frag&amp;gt; tag can be used to create a sandboxed environment for third-party JavaScript code to run in, this is a much easier way to address the security problem than to suggest we have to wait for ES-Harmony to add extra syntax to the language to accomplish the same thing.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 02:13, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Text/cache solution ==&lt;br /&gt;
&lt;br /&gt;
It seems to me that having a real text/cache script type, or a similar element that fires a load/error event and provides a .text property gets you where you need to be. You could setAttribute(&amp;quot;type&amp;quot;, &amp;quot;text/JavaScript&amp;quot;) or use the text to create a new script element. Or a new style element, or whatever. That puts you in control of when script or CSS is interpreted. With async as the default, you don&#039;t need a new ordered attribute as that&#039;s easily managed in script.&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;text/cache&amp;quot; response ==&lt;br /&gt;
&lt;br /&gt;
On the surface, this idea sounds like it&#039;s pretty similar to &amp;amp;lt;link rel=prefetch&amp;gt; -- in other words, we&#039;ve already standardized a way to prefetch a resource into cache, and probably in a better way than the hack that I did in LABjs of just relying on it fetching unrecognizable content.&lt;br /&gt;
&lt;br /&gt;
Unfortunately, it still suffers some similar problems to &amp;amp;lt;link rel=prefetch&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Firstly, if the notion is that you&#039;d use &amp;amp;lt;script type=&amp;quot;text/cache&amp;quot;&amp;gt; to prefetch, and then you&#039;d make a second request for the resource with a real &amp;amp;lt;script type=&amp;quot;text/javascript&amp;gt; node, it suffers the faulty assumption that the resource was served with proper cache headers.&lt;br /&gt;
&lt;br /&gt;
If we try to avoid the need for the second request by suggesting that you can access the `text` property of the node (and then eval() or inject that code when you want to execute it), this runs afoul of the same-origin policy problem if you requested the script from a non-local domain.&lt;br /&gt;
&lt;br /&gt;
The part of your suggestion that holds water would be if, given a &amp;quot;prefetched&amp;quot; script node that didn&#039;t execute yet, I could simply change that node&#039;s `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;, and that would explicitly trigger the browser to immediately execute it. &lt;br /&gt;
&lt;br /&gt;
This is an interesting idea. I can see though that it perhaps has some potential complications. &lt;br /&gt;
&lt;br /&gt;
For instance, would the browser have to keep a &amp;quot;hasExecuted&amp;quot; internal flag, so that changing the `type` value would only at most result in one execution? Also, if the script node in question is loaded but not executed, can the page alter the script code (by setting its `text` property) before making the change to the `type` value? In that case, should the browser execute the original code or the modified code? I can see security implications and confusion abounding in that behavior.&lt;br /&gt;
&lt;br /&gt;
Lastly, as mentioned above, this basically copies (in a similar but not exactly identical way) the &amp;amp;lt;link rel=prefetch&amp;gt; behavior. It may be seen as a negative to be trying to step on the toes of &amp;amp;lt;link rel=prefetch&amp;gt;, and also it may be more confusing to the general public as to why you&#039;d use one technique or the other, given their differences being so nuanced.&lt;br /&gt;
&lt;br /&gt;
That having been said, I can see that it&#039;s a viable alternate proposal. It&#039;s worth adding a note to that section of the main Wiki page and to getting some feedback from browser vendors on if they feel this is a more realistic/suitable solution to the use-case.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #eee&amp;quot;&amp;gt;&lt;br /&gt;
The difference between pre-fetching and this proposal seems pretty clear: Prefetching, if not performed now, will eventually be done once the user explicitly requests the page. while the &amp;quot;text/cache&amp;quot; solution indicates that the resource should load &#039;&#039;&#039;immediately&#039;&#039;&#039;, not during idle time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:10, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 20:56, 4 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== RequireJS order support ==&lt;br /&gt;
&lt;br /&gt;
I am the main developer for RequireJS, and just want to voice my support for a solution that works on the script element itself, either by leveraging the async attribute or allowing something like an &amp;quot;order&amp;quot; boolean attribute that would allow specifying a dynamically added script element should be executed in the order that it appears in the DOM. This capability should be something can be capability detected to avoid browser sniffing.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
Why shouldn&#039;t dependency management be implemented in high-level code and the browser simply provide a means by which parallel loading can be achieved?  As you know, there is a considerable amount of nuance.  I&#039;m of the opinion that implementing dependency management in the engine will eventually lead us right back to where we were 10 years ago with inline script tags which provides an easy method for dependency management at the expense of unmanaged blocking and an inability to detect or adapt to errors. &amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:51, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
There are many scripts on the web today that have implicit dependencies that should be loaded in a specific order, but the application developer will want to use dynamically created script elements for best performance. jQuery plugins that depend on the jQuery script already been executed in the page are a good example.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
Agreed, but I don&#039;t believe the proposals allow for proper emulation of the dependency management provided by script tags.  Prototype.js, for example, loads its core modules dynamically (using document.write so maybe not the best example...).  When prototype.js is included via script tags, ensuing script blocks are free to &amp;quot;depend&amp;quot; on all of these modules; Dynamically creating a dependency on prototype.js by loading both with the `async=true` property will fail since prototype.js won&#039;t be able to &amp;quot;jump the queue&amp;quot; and add insure its core modules load before its extensions.  &amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:51, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
I have to admit, you raise a valid point with respect to that common usage. The YUI model (where the framework is responsible for all loading) is perhaps the more appropriate model to move toward, though. Or, the framework doesn&#039;t auto-load its own dependencies at all. In either case, I think it&#039;s cleaner to have one loading mechanism in play rather than two or more competing ones, because as you point out, this is going to get messy. I still am not sure this means that the async=false proposal is lacking, because as I think about how complicated such a split loading would be with your `readyState` proposal, that seems even worse/more complicated to me. [[User:Getify|Getify]] 20:06, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
I would not argue &#039;&#039;&#039;against&#039;&#039;&#039; a global queue for synchronized execution, I just think the usefulness of such a construct declines as scripts that &amp;quot;I don&#039;t care about&amp;quot; are added.  It&#039;s my opinion the spec should provide the ability for high-level code to implement similar constructs, regardless of how complex it may be to do so. &amp;lt;br/&amp;gt; &lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
That being said, I don&#039;t think it&#039;s any more complicated than existing models.  Here&#039;s a demonstration of an [http://digital-fulcrum.com/executionorder/ IE-only parallel-loading, synchronously-executing script queue] implemented in 40 lines of code.  It tests both cached and un-cached scripts (from the LABjs test suite) and has been tested and found to be working in IE6-8. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[User:Serverherder|Serverherder]] 01:52, 16 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
- James Burke&lt;br /&gt;
&lt;br /&gt;
== Vendors Should Implement the Existing Suggestion ==&lt;br /&gt;
&lt;br /&gt;
Since the spec all ready includes a suggestion to download resources when the SRC attribute is set, parallel loading is an option the &#039;&#039;&#039;vendor&#039;&#039;&#039; chooses to implement and not something a script loader should be able to require.  Vendors that don&#039;t implement the suggestion will run the risk of being less performant in these situations.  That, however, is &#039;&#039;&#039;their&#039;&#039;&#039; choice to make as the spec suggests.  If the benefits of parallel loading dependencies are so great, users will opt for this in their choice of agent or vendors can adopt the existing mechanism.&lt;br /&gt;
&lt;br /&gt;
== The Current Suggestion Should be Changed to a Mandate ==&lt;br /&gt;
&lt;br /&gt;
The spec is pretty clear on why it chose to make this optional.  If dependent script loading is so common that browsers that adopt the suggestion enjoy considerably better performance, the user will eventually decide to opt for those implementations.  On the other hand, if it is more common that scripts are created and never inserted, leading to wasted bandwidth and clogged fetching queues, the converse will occur.  Either way, by making it optional, the spec hedges its bet and is more prone to be future-proof as the habits of script developers evolve.&lt;br /&gt;
&lt;br /&gt;
== Modify the specification so parallel-loading is configurable  ==&lt;br /&gt;
The specification expresses the reasons parallel loading should not be done.  To this point, allowing developers a means to &#039;&#039;&#039;require&#039;&#039;&#039; resources be downloaded in parallel is in direct opposition to the spec&#039;s all ready stated position.  To this end, I see no purpose for async=true: A browser that hopes to be highly performant should implement the spec&#039;s suggestion to improve performance.  Developers should not be able to circumvent the vendor&#039;s attempt to be conservative in its use of bandwidth simply to achieve ordered execution.&lt;br /&gt;
&lt;br /&gt;
=== response ===&lt;br /&gt;
Nothing about this proposal requires any parallelism that the browser wouldn&#039;t already afford. The spirit of this proposal is not about loading behavior, but about execution order.&lt;br /&gt;
&lt;br /&gt;
The spec says nothing about the parallel loading, nor do I think it should necessarily. All modern browsers HAVE in fact implemented parallel loading for all scripts, regardless of async or not. And I think this is a good thing. But it has nothing to do with `async` or my proposal to make the definition of `async` symmetric/consistent between markup script elements and dynamic script elements.&lt;br /&gt;
&lt;br /&gt;
The spec already defines that `async` is specifically to create unblocking of the page&#039;s resource loading AND &amp;quot;as soon as possible&amp;quot; execution of the script. But it only defines this for parser-inserted (markup) script tags. I think this is short-sighted, because I think a script-loader should be able to opt into either ordered or unordered execution in the same way that markup script elements can. That&#039;s really all the main proposal is about.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#EEE&amp;quot;&amp;gt;&lt;br /&gt;
Script loaders can all ready choose ordered execution: chain the onload events.  The issue is that vendors have chosen &#039;&#039;&#039;not&#039;&#039;&#039; to adopt the specification&#039;s suggestion. The high priority vendors place on performance suggests this was not done consciously.  Instead of advocating WHATWG for additions to the spec, I think we may be better off advocating vendors implement the spec&#039;s suggestion.  The existing implementation of parallelism for parser-inserted scripts suggests vendors would be willing to do so and the existing lack of support is merely an issue of communication.  &lt;br /&gt;
&lt;br /&gt;
At its core, the existing suggestion to retrieve upon src assignment is not altogether different than the eager-fetching performed for parser-inserted scripts.  The risk still exists that the browser download a script that is never actually executed.  For example, below a bunch of script tags, the HTML may contain a closing comment tag (--&amp;gt;) and one of the scripts doc.write(&amp;quot;&amp;lt;!--&amp;quot;).  Another risk is that the eager-fetching clog the fetch queue, delaying the download of any scripts included via document.write().    Vendors have &#039;&#039;&#039;chosen&#039;&#039;&#039; to assume this risk though, since its probability is low and the benefit high.  &lt;br /&gt;
&lt;br /&gt;
Likewise, vendors can calculate the risk/reward for script-inserted scripts and will likely make the same decision -- they implement this exact behavior for images.  Script loaders should respect the fact that a vendor has consciously chosen &#039;&#039;&#039;not&#039;&#039;&#039; to support parallelism for script-inserted scripts.  Providing a means by which this decision can be circumvented undermines their choice.   I believe it was in that spirit the specification leaves eager fetching script resources as a suggestion.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
I think you still miss the pragmatic point here. I&#039;m not disagreeing with you in theory, but in practice, the hard-nosed approach you suggest would spell death for any script loader. That&#039;s not a chance I&#039;m willing to take.&lt;br /&gt;
&lt;br /&gt;
When a developer uses a script loader that claims it is &amp;quot;The Performance Script Loader&amp;quot; (as LABjs does), and they test in some latest-version browser (whether that be FF, Safari, Chrome, IE, or Opera) and the performance is terrible (worse than before they installed LABjs), they&#039;re not going to say, &amp;quot;Well, I&#039;ll just keep LABjs in there and hope those browsers fix this quickly&amp;quot;. &lt;br /&gt;
&lt;br /&gt;
This is tail-wagging-the-dog mentality, and it ignores the reality: people will almost always chose the path of least resistance. If they try a script loader, and it gives them worse performance than before, they&#039;ll simply take it out. They may complain about it once in a tweet, but they won&#039;t take a stand against the browsers to the detriment of their own site and their own users, and they won&#039;t tell all their users to switch to another browser in protest.&lt;br /&gt;
&lt;br /&gt;
And worse, for LABjs, the path of least resistance may very well mean that if performance is important to them, they&#039;ll just pick some other loader, which has more hacks in it. As long as they get what they want, which is reliable and pretty good performance, they&#039;ll be happy. And it will totally have been lost on them that the battle for web standards was just dealt a blow by their actions. General web page authors do not care to fight that battle on behalf of LABjs. So, LABjs must fight that battle under the covers.&lt;br /&gt;
&lt;br /&gt;
I understand your perspective, but I can say as the maintainer of a tool like LABjs that cares about keeping its relevancy for &amp;quot;survival&amp;quot;, I won&#039;t take an action that I know is likely to cause a decline in the usefulness of my tool. Instead, I will take a more cautious, slow-to-change approach, while at the same time feature-testing for the new standards and using them if available. Only after a long time of the new feature-testable stuff being present will I consider removing the legacy stuff. This is a strategy I think almost every framework/lib maintainer would echo support for.&lt;br /&gt;
&lt;br /&gt;
~~~~&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 21:39, 23 December 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Talk:Dynamic_Script_Execution_Order&amp;diff=5674</id>
		<title>Talk:Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Talk:Dynamic_Script_Execution_Order&amp;diff=5674"/>
		<updated>2010-12-23T21:39:16Z</updated>

		<summary type="html">&lt;p&gt;Getify: response about `async`&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please discuss the proposal here. All informal feedback is welcomed, and when it&#039;s appropriate, please add content to the main page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== What about CommonJS / RequireJS? ==&lt;br /&gt;
&lt;br /&gt;
I&#039;m not sure we need to complicate the HTML or browser engines in order to satisfy the advanced use cases.  To me it seems that the JavaScript langauge itself could abstract the requirements and let the browser vendors implement whatever solution they see fit.  Specifically, if JavaScript provided the define() and require() calls proposed in the CommonJS &amp;quot;specifications&amp;quot; (and implemented by [http://requirejs.org/docs/api.html RequireJS], [http://svn.dojotoolkit.org/src/dojo/trunk/_base/_loader/loader.js dojo (trunk)], and others *Yabble?), then browsers could just load and evaluate scripts as fast as possible without regard to script execution order.  &lt;br /&gt;
&lt;br /&gt;
This would solve all of the problems except for the current inability to use loaders like LABjs in FF4 or webkit.  To solve that problem, we should consider keeping the existing functionality or triggering the deprecated functionality when needed.&lt;br /&gt;
&lt;br /&gt;
--[[User:Unscriptable|Unscriptable]] 01:29, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Response ===&lt;br /&gt;
&lt;br /&gt;
This is a valid point except that it assumes that all code that needs to be loaded is &amp;quot;modular&amp;quot;... in fact, most code on the internet that needs to be loaded is NOT modular, and instead requires particular ordering when being loaded... for instance, &amp;quot;jquery&amp;quot; and &amp;quot;jquery-ui&amp;quot; -- jQuery must be run before jQuery-UI is loaded and run.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The spirit of this proposal is to add the facility to native browser (rendering engine, specifically) behavior, such that any and all existing content is able to be loaded (even with dependencies and order) without modification.&lt;br /&gt;
&amp;lt;blockquote style=background-color:#eee&amp;gt;&lt;br /&gt;
Are we trying to codify how one would implement this HTML dynamically so I can achieve parallel loading but retain dependency that is inferred in the order? &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: white&amp;quot;&amp;gt; &lt;br /&gt;
&amp;amp;lt;script src=&amp;quot;jquery.js&amp;quot;&amp;amp;gt; &amp;amp;lt;/script&amp;amp;gt; &amp;lt;br/&amp;gt; &lt;br /&gt;
&amp;amp;lt;script src=&amp;quot;jquery-ui.js&amp;quot;&amp;amp;gt; &amp;amp;lt;/script&amp;amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If so, we must consider the case in which the dependency itself loads additional code which the discussions around `async=false` or even text/cache do not account for.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:23, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dependent code should be modular ===&lt;br /&gt;
&lt;br /&gt;
I completely agree, 99% of JavaScript in use today is NOT modular, but that&#039;s arguably because there was no built-in support to make it modular (IMHO, YMMV, RTFM, BBQ, ROFL).  But that doesn&#039;t mean there aren&#039;t dependencies that span script files / script nodes.  I don&#039;t know how many sites would be broken (or if there is any way to determine how many sites would be broken) if/when the latest browsers started loading and evaluating script-injected scripts out of order.  It&#039;s not just LABjs-based sites.  I&#039;ll bet many dojo and YUI sites will break, too (although I don&#039;t know this for a fact because I don&#039;t know exactly how their dynamic script loaders work).  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;In any case, breaking existing sites is a BAD IDEA. :)  There should be some way to allow LABjs and other dynamic script loaders to continue to work.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The part I don&#039;t agree with in the proposals is that it should be the job of HTML and/or the browser engine to manage dependencies.  Specifically, I am talking about the scriptGroup element and the &amp;quot;waitFor&amp;quot; attribute.  The &amp;quot;async&amp;quot; attribute may be the right level: it appropriates only general heuristics to the browser: &#039;&#039;load these scripts whenever you like, but load these other scripts in this order&#039;&#039;.  &lt;br /&gt;
&lt;br /&gt;
Detailed dependency management belongs in the modules, though.  This is how it&#039;s done in most environments / languages: import statements, include statements, etc.  It&#039;s no different in JavaScript, if a developer wants to take advantage of some code in another module, she/he should import it into their current module.  This should be the model moving forward.  It&#039;s not hard, it&#039;s not un-web-like, and it&#039;s already being done.  &lt;br /&gt;
&lt;br /&gt;
Existing code can be made to work modularly, too:&lt;br /&gt;
&lt;br /&gt;
1. Identify modules and wrap them with a define() method.&lt;br /&gt;
2. Identify dependencies using either the [http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition Asynchronous Module Definition] format or the CommonJS synchronous format&lt;br /&gt;
&lt;br /&gt;
async format:&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
define([&#039;dependency1&#039;, &#039;dependency2&#039;], function (dependency1, dependency2) {&lt;br /&gt;
	/* this is the factory function, return the module inside here */&lt;br /&gt;
	return { /* some object, but could also be a constructor or a function */ };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
sync format:&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
define(function (require, exports, module) {&lt;br /&gt;
	/* this is the factory function, require dependencies and return the module inside here */&lt;br /&gt;
	var dependency1 = require(&#039;dependency1&#039;);&lt;br /&gt;
	var dependency2 = require(&#039;dependency2&#039;);&lt;br /&gt;
	return { /* some object, but could also be a constructor or a function */ };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the async case, the browser may employ whatever method it sees fit (e.g. parallel) to download the dependencies since the code inside the factory function won&#039;t execute until all of the dependencies are loaded and evaluated.  In the sync case, the browser has no choice but to download and evaluate the dependency before continuing code execution.  It&#039;s conceivable that the browser could find and asynchronously pre-fetch the inline require()&#039;ed dependencies in the sync case.  (RequireJS doesn&#039;t quite do this.  Instead I believe it finds the inline require() calls and converts the module to an async module.)  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;So, in short: don&#039;t break LABjs (or any other script loaders), but don&#039;t extend the spec to manage dependency management from HTML.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
--[[User:Unscriptable|Unscriptable]] 01:29, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Response #2 ==&lt;br /&gt;
&lt;br /&gt;
The cat&#039;s already out of the bag... we&#039;ve got a dozen+ years worth, hundreds of millions of javascript files out there, which are NOT modular, and will not be modified to be modular. &lt;br /&gt;
&lt;br /&gt;
If we&#039;re talking about either trying to change all the JS on the web to be modular, or trying to extend the HTML markup spec to include a better facility for accomodating the existing script content, I think the former is idealistic and &amp;quot;correct&amp;quot; but the latter is realistic and &amp;quot;right&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
A modular loading system will only work for modular code, but a general loading mechanism in HTML (like the &amp;amp;lt;script&amp;gt; tag) can load both modular and non-modular code, which is why that more general mechanism is what&#039;s in HTML right now and not some more specialized loading API that only loads a small percentage of JavaScript code out there.&lt;br /&gt;
&lt;br /&gt;
The &amp;amp;lt;script&amp;gt; tag has worked fine forever, and there&#039;s been no real big push to do away with it in favor of having all loading handled by JavaScript -- in fact, that would be impossible because if you had no way to load JS onto the page with a &amp;amp;lt;script&amp;gt; tag, then no JavaScript could run on a page to load more code. So I don&#039;t agree that we should stop adding functionality to the HTML loading mechanism and only focus on JavaScript.&lt;br /&gt;
&lt;br /&gt;
If we agree that the &amp;amp;lt;script&amp;gt; tag itself is not only required but normative, then I&#039;m not sure why decorating the &amp;amp;lt;script&amp;gt; tag with additional behavior to help it be more powerful is a bad pattern. We&#039;ve addded &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; already. I&#039;m not sure I understand why adding a wrapper tag called &amp;quot;scriptGroup&amp;quot; would be that much different than attributes on the &amp;amp;lt;script&amp;gt; tag?&lt;br /&gt;
&lt;br /&gt;
I also don&#039;t think that &amp;quot;scriptGroup&amp;quot; constitutes &amp;quot;dependency management&amp;quot;. &amp;quot;scriptGroup&amp;quot; is a semantic way for a web author to express that they want to group a set of scripts together ONLY so far as execution order behavior is concerned. It implies nothing more to it (that &amp;quot;dependency management&amp;quot; would imply) than that.&lt;br /&gt;
&lt;br /&gt;
For instance, consider this: if JavaScript had been built with non-global context and statement level context and sandboxing and such things, we&#039;d have a much more secure JavaScript right now. But just because JavaScript may someday be taught some new tricks in terms of security, that doesn&#039;t mean that &amp;quot;now&amp;quot; HTML shouldn&#039;t try to help solve the problem. If for instance the &amp;amp;lt;frag&amp;gt; tag can be used to create a sandboxed environment for third-party JavaScript code to run in, this is a much easier way to address the security problem than to suggest we have to wait for ES-Harmony to add extra syntax to the language to accomplish the same thing.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 02:13, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Text/cache solution ==&lt;br /&gt;
&lt;br /&gt;
It seems to me that having a real text/cache script type, or a similar element that fires a load/error event and provides a .text property gets you where you need to be. You could setAttribute(&amp;quot;type&amp;quot;, &amp;quot;text/JavaScript&amp;quot;) or use the text to create a new script element. Or a new style element, or whatever. That puts you in control of when script or CSS is interpreted. With async as the default, you don&#039;t need a new ordered attribute as that&#039;s easily managed in script.&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;text/cache&amp;quot; response ==&lt;br /&gt;
&lt;br /&gt;
On the surface, this idea sounds like it&#039;s pretty similar to &amp;amp;lt;link rel=prefetch&amp;gt; -- in other words, we&#039;ve already standardized a way to prefetch a resource into cache, and probably in a better way than the hack that I did in LABjs of just relying on it fetching unrecognizable content.&lt;br /&gt;
&lt;br /&gt;
Unfortunately, it still suffers some similar problems to &amp;amp;lt;link rel=prefetch&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Firstly, if the notion is that you&#039;d use &amp;amp;lt;script type=&amp;quot;text/cache&amp;quot;&amp;gt; to prefetch, and then you&#039;d make a second request for the resource with a real &amp;amp;lt;script type=&amp;quot;text/javascript&amp;gt; node, it suffers the faulty assumption that the resource was served with proper cache headers.&lt;br /&gt;
&lt;br /&gt;
If we try to avoid the need for the second request by suggesting that you can access the `text` property of the node (and then eval() or inject that code when you want to execute it), this runs afoul of the same-origin policy problem if you requested the script from a non-local domain.&lt;br /&gt;
&lt;br /&gt;
The part of your suggestion that holds water would be if, given a &amp;quot;prefetched&amp;quot; script node that didn&#039;t execute yet, I could simply change that node&#039;s `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;, and that would explicitly trigger the browser to immediately execute it. &lt;br /&gt;
&lt;br /&gt;
This is an interesting idea. I can see though that it perhaps has some potential complications. &lt;br /&gt;
&lt;br /&gt;
For instance, would the browser have to keep a &amp;quot;hasExecuted&amp;quot; internal flag, so that changing the `type` value would only at most result in one execution? Also, if the script node in question is loaded but not executed, can the page alter the script code (by setting its `text` property) before making the change to the `type` value? In that case, should the browser execute the original code or the modified code? I can see security implications and confusion abounding in that behavior.&lt;br /&gt;
&lt;br /&gt;
Lastly, as mentioned above, this basically copies (in a similar but not exactly identical way) the &amp;amp;lt;link rel=prefetch&amp;gt; behavior. It may be seen as a negative to be trying to step on the toes of &amp;amp;lt;link rel=prefetch&amp;gt;, and also it may be more confusing to the general public as to why you&#039;d use one technique or the other, given their differences being so nuanced.&lt;br /&gt;
&lt;br /&gt;
That having been said, I can see that it&#039;s a viable alternate proposal. It&#039;s worth adding a note to that section of the main Wiki page and to getting some feedback from browser vendors on if they feel this is a more realistic/suitable solution to the use-case.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #eee&amp;quot;&amp;gt;&lt;br /&gt;
The difference between pre-fetching and this proposal seems pretty clear: Prefetching, if not performed now, will eventually be done once the user explicitly requests the page. while the &amp;quot;text/cache&amp;quot; solution indicates that the resource should load &#039;&#039;&#039;immediately&#039;&#039;&#039;, not during idle time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:10, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 20:56, 4 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== RequireJS order support ==&lt;br /&gt;
&lt;br /&gt;
I am the main developer for RequireJS, and just want to voice my support for a solution that works on the script element itself, either by leveraging the async attribute or allowing something like an &amp;quot;order&amp;quot; boolean attribute that would allow specifying a dynamically added script element should be executed in the order that it appears in the DOM. This capability should be something can be capability detected to avoid browser sniffing.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
Why shouldn&#039;t dependency management be implemented in high-level code and the browser simply provide a means by which parallel loading can be achieved?  As you know, there is a considerable amount of nuance.  I&#039;m of the opinion that implementing dependency management in the engine will eventually lead us right back to where we were 10 years ago with inline script tags which provides an easy method for dependency management at the expense of unmanaged blocking and an inability to detect or adapt to errors. &amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:51, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
There are many scripts on the web today that have implicit dependencies that should be loaded in a specific order, but the application developer will want to use dynamically created script elements for best performance. jQuery plugins that depend on the jQuery script already been executed in the page are a good example.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
Agreed, but I don&#039;t believe the proposals allow for proper emulation of the dependency management provided by script tags.  Prototype.js, for example, loads its core modules dynamically (using document.write so maybe not the best example...).  When prototype.js is included via script tags, ensuing script blocks are free to &amp;quot;depend&amp;quot; on all of these modules; Dynamically creating a dependency on prototype.js by loading both with the `async=true` property will fail since prototype.js won&#039;t be able to &amp;quot;jump the queue&amp;quot; and add insure its core modules load before its extensions.  &amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:51, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
I have to admit, you raise a valid point with respect to that common usage. The YUI model (where the framework is responsible for all loading) is perhaps the more appropriate model to move toward, though. Or, the framework doesn&#039;t auto-load its own dependencies at all. In either case, I think it&#039;s cleaner to have one loading mechanism in play rather than two or more competing ones, because as you point out, this is going to get messy. I still am not sure this means that the async=false proposal is lacking, because as I think about how complicated such a split loading would be with your `readyState` proposal, that seems even worse/more complicated to me. [[User:Getify|Getify]] 20:06, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
 &lt;br /&gt;
I would not argue &#039;&#039;&#039;against&#039;&#039;&#039; a global queue for synchronized execution, I just think the usefulness of such a construct declines as scripts that &amp;quot;I don&#039;t care about&amp;quot; are added.  It&#039;s my opinion the spec should provide the ability for high-level code to implement similar constructs, regardless of how complex it may be to do so. &amp;lt;br/&amp;gt; &lt;br /&gt;
&amp;lt;br&amp;gt; &lt;br /&gt;
That being said, I don&#039;t think it&#039;s any more complicated than existing models.  Here&#039;s a demonstration of an [http://digital-fulcrum.com/executionorder/ IE-only parallel-loading, synchronously-executing script queue] implemented in 40 lines of code.  It tests both cached and un-cached scripts (from the LABjs test suite) and has been tested and found to be working in IE6-8. &lt;br /&gt;
&amp;lt;br&amp;gt;&lt;br /&gt;
[[User:Serverherder|Serverherder]] 01:52, 16 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
- James Burke&lt;br /&gt;
&lt;br /&gt;
== Vendors Should Implement the Existing Suggestion ==&lt;br /&gt;
&lt;br /&gt;
Since the spec all ready includes a suggestion to download resources when the SRC attribute is set, parallel loading is an option the &#039;&#039;&#039;vendor&#039;&#039;&#039; chooses to implement and not something a script loader should be able to require.  Vendors that don&#039;t implement the suggestion will run the risk of being less performant in these situations.  That, however, is &#039;&#039;&#039;their&#039;&#039;&#039; choice to make as the spec suggests.  If the benefits of parallel loading dependencies are so great, users will opt for this in their choice of agent or vendors can adopt the existing mechanism.&lt;br /&gt;
&lt;br /&gt;
== The Current Suggestion Should be Changed to a Mandate ==&lt;br /&gt;
&lt;br /&gt;
The spec is pretty clear on why it chose to make this optional.  If dependent script loading is so common that browsers that adopt the suggestion enjoy considerably better performance, the user will eventually decide to opt for those implementations.  On the other hand, if it is more common that scripts are created and never inserted, leading to wasted bandwidth and clogged fetching queues, the converse will occur.  Either way, by making it optional, the spec hedges its bet and is more prone to be future-proof as the habits of script developers evolve.&lt;br /&gt;
&lt;br /&gt;
== Modify the specification so parallel-loading is configurable  ==&lt;br /&gt;
The specification expresses the reasons parallel loading should not be done.  To this point, allowing developers a means to &#039;&#039;&#039;require&#039;&#039;&#039; resources be downloaded in parallel is in direct opposition to the spec&#039;s all ready stated position.  To this end, I see no purpose for async=true: A browser that hopes to be highly performant should implement the spec&#039;s suggestion to improve performance.  Developers should not be able to circumvent the vendor&#039;s attempt to be conservative in its use of bandwidth simply to achieve ordered execution.&lt;br /&gt;
&lt;br /&gt;
=== response ===&lt;br /&gt;
Nothing about this proposal requires any parallelism that the browser wouldn&#039;t already afford. The spirit of this proposal is not about loading behavior, but about execution order.&lt;br /&gt;
&lt;br /&gt;
The spec says nothing about the parallel loading, nor do I think it should necessarily. All modern browsers HAVE in fact implemented parallel loading for all scripts, regardless of async or not. And I think this is a good thing. But it has nothing to do with `async` or my proposal to make the definition of `async` symmetric/consistent between markup script elements and dynamic script elements.&lt;br /&gt;
&lt;br /&gt;
The spec already defines that `async` is specifically to create unblocking of the page&#039;s resource loading AND &amp;quot;as soon as possible&amp;quot; execution of the script. But it only defines this for parser-inserted (markup) script tags. I think this is short-sighted, because I think a script-loader should be able to opt into either ordered or unordered execution in the same way that markup script elements can. That&#039;s really all the main proposal is about.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 21:39, 23 December 2010 (UTC)&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5668</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5668"/>
		<updated>2010-12-23T19:32:36Z</updated>

		<summary type="html">&lt;p&gt;Getify: updating comments around the &amp;quot;preloading&amp;quot; technique IE (and the spec) give&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, &amp;lt;del&amp;gt;and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&amp;lt;/del&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Update: the behavior *is* in fact in the spec as a suggestion (aka, with &amp;quot;may&amp;quot; instead of &amp;quot;should&amp;quot;). The wording in question is located in [http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML5 4.3.1, &amp;quot;Running A Script&amp;quot; algorithm] step, 12, which states:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#f1dac9&amp;quot;&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 19:32, 23 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be [[Dynamic_Script_Execution_Order#readyState_.22preloading.22|listed as such]]. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=620852 Mozilla Bug #620852]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
If at any point these services begin dynamically loading `async=false` scripts of their own, they will begin blocking.  This can lead to unexpected behavior of existing code as third-party libraries and services change.  It would seem the specification should provide a means by which this blocking can be isolated to dependencies defined by the author and ordered execution not introduce conditions that allow unrelated libraries to affect this.   &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Need Attribute/Property Semantic Inconsistency ====&lt;br /&gt;
In order to achieve consistency between the semantics of how the parser-inserted script element&#039;s `async` attribute and the script-inserted script element&#039;s `async` property behave, a slight change needs to be made to the value parsing/interpretation for parser-inserted script elements&#039; `async` attribute.&lt;br /&gt;
&lt;br /&gt;
If a parser-inserted script element has the `async` attribute present, but its value is exactly &amp;quot;false&amp;quot; (or any capitalization thereof), the script element should behave EXACTLY as if no `async` attribute were present.&lt;br /&gt;
&lt;br /&gt;
If the attribute is present and either has no value, or has any other value except &amp;quot;false&amp;quot; (for instance, &amp;quot;true&amp;quot; or &amp;quot;async&amp;quot;), then it should be interpreted to be turning on &amp;quot;async&amp;quot; for that element.&lt;br /&gt;
&lt;br /&gt;
Because it is highly unlikely (and indeed, quite irrational) that any existing web content is doing `async=&amp;quot;false&amp;quot;` in HTML markup and yet still expecting &amp;quot;async&amp;quot; behavior to be turned on, it is pretty unlikely that this minor interpretation change will cause backwards-compatibility issues.&lt;br /&gt;
[[User:Getify|Getify]] 03:05, 22 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
And does the onload event fire again after the script executes?  &amp;lt;br/&amp;gt; &lt;br /&gt;
Or only when it&#039;s loaded? &amp;lt;br/&amp;gt; &lt;br /&gt;
Both seem to violate current `onload` semantics.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 20:27, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
As far as I&#039;m concerned, I think the &amp;quot;load&amp;quot; event should happen immediately after a script loads, but before it executes, as I spell out in the [[Dynamic_Script_Execution_Order#Proposal_Amendment:_Events|Events proposal ammendment section]]. [[User:Getify|Getify]] 20:33, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question. This alternate proposal is very similar in spirit to the [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|previous section on explicit support for &amp;quot;text/cache&amp;quot;]] mime-type value.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Update&#039;&#039;&#039; [[User:Getify|Getify]] 14:10, 20 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This seems to be the spec stating that browsers may do what IE&#039;s behavior on scripts does, which is to start fetching the resource once the `src` property is set, but not execute it until the script element is inserted into the DOM.&lt;br /&gt;
&lt;br /&gt;
It would seem then that browsers could/should be petitioned to consider implementing this suggestion, perhaps taking the `readyState` implementation from IE as guidance. While I&#039;m still not sure that this would be my preferred method of solving the overall use-case in discussion on this page, it would be an option for such, and it would also help other use-cases, such as those being discussed in this W3C public-html thread:&lt;br /&gt;
&lt;br /&gt;
[http://lists.w3.org/Archives/Public/public-html/2010Dec/0174.html Need to: &amp;quot;preload&amp;quot; CSS and JS]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5645</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5645"/>
		<updated>2010-12-22T03:07:10Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding link to mozilla bug about attribute/property consistency&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=620852 Mozilla Bug #620852]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
If at any point these services begin dynamically loading `async=false` scripts of their own, they will begin blocking.  This can lead to unexpected behavior of existing code as third-party libraries and services change.  It would seem the specification should provide a means by which this blocking can be isolated to dependencies defined by the author and ordered execution not introduce conditions that allow unrelated libraries to affect this.   &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Need Attribute/Property Semantic Inconsistency ====&lt;br /&gt;
In order to achieve consistency between the semantics of how the parser-inserted script element&#039;s `async` attribute and the script-inserted script element&#039;s `async` property behave, a slight change needs to be made to the value parsing/interpretation for parser-inserted script elements&#039; `async` attribute.&lt;br /&gt;
&lt;br /&gt;
If a parser-inserted script element has the `async` attribute present, but its value is exactly &amp;quot;false&amp;quot; (or any capitalization thereof), the script element should behave EXACTLY as if no `async` attribute were present.&lt;br /&gt;
&lt;br /&gt;
If the attribute is present and either has no value, or has any other value except &amp;quot;false&amp;quot; (for instance, &amp;quot;true&amp;quot; or &amp;quot;async&amp;quot;), then it should be interpreted to be turning on &amp;quot;async&amp;quot; for that element.&lt;br /&gt;
&lt;br /&gt;
Because it is highly unlikely (and indeed, quite irrational) that any existing web content is doing `async=&amp;quot;false&amp;quot;` in HTML markup and yet still expecting &amp;quot;async&amp;quot; behavior to be turned on, it is pretty unlikely that this minor interpretation change will cause backwards-compatibility issues.&lt;br /&gt;
[[User:Getify|Getify]] 03:05, 22 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
And does the onload event fire again after the script executes?  &amp;lt;br/&amp;gt; &lt;br /&gt;
Or only when it&#039;s loaded? &amp;lt;br/&amp;gt; &lt;br /&gt;
Both seem to violate current `onload` semantics.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 20:27, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
As far as I&#039;m concerned, I think the &amp;quot;load&amp;quot; event should happen immediately after a script loads, but before it executes, as I spell out in the [[Dynamic_Script_Execution_Order#Proposal_Amendment:_Events|Events proposal ammendment section]]. [[User:Getify|Getify]] 20:33, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question. This alternate proposal is very similar in spirit to the [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|previous section on explicit support for &amp;quot;text/cache&amp;quot;]] mime-type value.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Update&#039;&#039;&#039; [[User:Getify|Getify]] 14:10, 20 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This seems to be the spec stating that browsers may do what IE&#039;s behavior on scripts does, which is to start fetching the resource once the `src` property is set, but not execute it until the script element is inserted into the DOM.&lt;br /&gt;
&lt;br /&gt;
It would seem then that browsers could/should be petitioned to consider implementing this suggestion, perhaps taking the `readyState` implementation from IE as guidance. While I&#039;m still not sure that this would be my preferred method of solving the overall use-case in discussion on this page, it would be an option for such, and it would also help other use-cases, such as those being discussed in this W3C public-html thread:&lt;br /&gt;
&lt;br /&gt;
[http://lists.w3.org/Archives/Public/public-html/2010Dec/0174.html Need to: &amp;quot;preload&amp;quot; CSS and JS]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5644</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5644"/>
		<updated>2010-12-22T03:05:57Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding proposal amendment: regarding async attribute/property consistency&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
If at any point these services begin dynamically loading `async=false` scripts of their own, they will begin blocking.  This can lead to unexpected behavior of existing code as third-party libraries and services change.  It would seem the specification should provide a means by which this blocking can be isolated to dependencies defined by the author and ordered execution not introduce conditions that allow unrelated libraries to affect this.   &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Need Attribute/Property Semantic Inconsistency ====&lt;br /&gt;
In order to achieve consistency between the semantics of how the parser-inserted script element&#039;s `async` attribute and the script-inserted script element&#039;s `async` property behave, a slight change needs to be made to the value parsing/interpretation for parser-inserted script elements&#039; `async` attribute.&lt;br /&gt;
&lt;br /&gt;
If a parser-inserted script element has the `async` attribute present, but its value is exactly &amp;quot;false&amp;quot; (or any capitalization thereof), the script element should behave EXACTLY as if no `async` attribute were present.&lt;br /&gt;
&lt;br /&gt;
If the attribute is present and either has no value, or has any other value except &amp;quot;false&amp;quot; (for instance, &amp;quot;true&amp;quot; or &amp;quot;async&amp;quot;), then it should be interpreted to be turning on &amp;quot;async&amp;quot; for that element.&lt;br /&gt;
&lt;br /&gt;
Because it is highly unlikely (and indeed, quite irrational) that any existing web content is doing `async=&amp;quot;false&amp;quot;` in HTML markup and yet still expecting &amp;quot;async&amp;quot; behavior to be turned on, it is pretty unlikely that this minor interpretation change will cause backwards-compatibility issues.&lt;br /&gt;
[[User:Getify|Getify]] 03:05, 22 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
And does the onload event fire again after the script executes?  &amp;lt;br/&amp;gt; &lt;br /&gt;
Or only when it&#039;s loaded? &amp;lt;br/&amp;gt; &lt;br /&gt;
Both seem to violate current `onload` semantics.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 20:27, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
As far as I&#039;m concerned, I think the &amp;quot;load&amp;quot; event should happen immediately after a script loads, but before it executes, as I spell out in the [[Dynamic_Script_Execution_Order#Proposal_Amendment:_Events|Events proposal ammendment section]]. [[User:Getify|Getify]] 20:33, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question. This alternate proposal is very similar in spirit to the [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|previous section on explicit support for &amp;quot;text/cache&amp;quot;]] mime-type value.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Update&#039;&#039;&#039; [[User:Getify|Getify]] 14:10, 20 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This seems to be the spec stating that browsers may do what IE&#039;s behavior on scripts does, which is to start fetching the resource once the `src` property is set, but not execute it until the script element is inserted into the DOM.&lt;br /&gt;
&lt;br /&gt;
It would seem then that browsers could/should be petitioned to consider implementing this suggestion, perhaps taking the `readyState` implementation from IE as guidance. While I&#039;m still not sure that this would be my preferred method of solving the overall use-case in discussion on this page, it would be an option for such, and it would also help other use-cases, such as those being discussed in this W3C public-html thread:&lt;br /&gt;
&lt;br /&gt;
[http://lists.w3.org/Archives/Public/public-html/2010Dec/0174.html Need to: &amp;quot;preload&amp;quot; CSS and JS]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5641</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5641"/>
		<updated>2010-12-20T14:10:58Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding comment about spec&amp;#039;s support for the `readyState` proposal&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
If at any point these services begin dynamically loading `async=false` scripts of their own, they will begin blocking.  This can lead to unexpected behavior of existing code as third-party libraries and services change.  It would seem the specification should provide a means by which this blocking can be isolated to dependencies defined by the author and ordered execution not introduce conditions that allow unrelated libraries to affect this.   &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
And does the onload event fire again after the script executes?  &amp;lt;br/&amp;gt; &lt;br /&gt;
Or only when it&#039;s loaded? &amp;lt;br/&amp;gt; &lt;br /&gt;
Both seem to violate current `onload` semantics.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 20:27, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
As far as I&#039;m concerned, I think the &amp;quot;load&amp;quot; event should happen immediately after a script loads, but before it executes, as I spell out in the [[Dynamic_Script_Execution_Order#Proposal_Amendment:_Events|Events proposal ammendment section]]. [[User:Getify|Getify]] 20:33, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question. This alternate proposal is very similar in spirit to the [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|previous section on explicit support for &amp;quot;text/cache&amp;quot;]] mime-type value.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Update&#039;&#039;&#039; [[User:Getify|Getify]] 14:10, 20 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
[http://www.w3.org/TR/html5/scripting-1.html#running-a-script HTML Spec, 4.3.1, &#039;Running a script&#039; algorithm]&lt;br /&gt;
&lt;br /&gt;
Specifically, in step 12 (which is about fetching a `src` URL):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote&amp;gt;&lt;br /&gt;
For performance reasons, user agents may start fetching the script as soon as the attribute is set, instead, in the hope that the element will be inserted into the document. Either way, once the element is inserted into the document, the load must have started. If the UA performs such prefetching, but the element is never inserted in the document, or the src attribute is dynamically changed, then the user agent will not execute the script, and the fetching process will have been effectively wasted.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
This seems to be the spec stating that browsers may do what IE&#039;s behavior on scripts does, which is to start fetching the resource once the `src` property is set, but not execute it until the script element is inserted into the DOM.&lt;br /&gt;
&lt;br /&gt;
It would seem then that browsers could/should be petitioned to consider implementing this suggestion, perhaps taking the `readyState` implementation from IE as guidance. While I&#039;m still not sure that this would be my preferred method of solving the overall use-case in discussion on this page, it would be an option for such, and it would also help other use-cases, such as those being discussed in this W3C public-html thread:&lt;br /&gt;
&lt;br /&gt;
[http://lists.w3.org/Archives/Public/public-html/2010Dec/0174.html Need to: &amp;quot;preload&amp;quot; CSS and JS]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5629</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5629"/>
		<updated>2010-12-15T20:33:27Z</updated>

		<summary type="html">&lt;p&gt;Getify: reply comment regarding &amp;quot;load&amp;quot; event semantics&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
If at any point these services begin dynamically loading `async=false` scripts of their own, they will begin blocking.  This can lead to unexpected behavior of existing code as third-party libraries and services change.  It would seem the specification should provide a means by which this blocking can be isolated to dependencies defined by the author and ordered execution not introduce conditions that allow unrelated libraries to affect this.   &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
And does the onload event fire again after the script executes?  &amp;lt;br/&amp;gt; &lt;br /&gt;
Or only when it&#039;s loaded? &amp;lt;br/&amp;gt; &lt;br /&gt;
Both seem to violate current `onload` semantics.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 20:27, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
As far as I&#039;m concerned, I think the &amp;quot;load&amp;quot; event should happen immediately after a script loads, but before it executes, as I spell out in the [[Dynamic_Script_Execution_Order#Proposal_Amendment:_Events|Events proposal ammendment section]]. [[User:Getify|Getify]] 20:33, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question. This alternate proposal is very similar in spirit to the [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|previous section on explicit support for &amp;quot;text/cache&amp;quot;]] mime-type value.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Talk:Dynamic_Script_Execution_Order&amp;diff=5627</id>
		<title>Talk:Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Talk:Dynamic_Script_Execution_Order&amp;diff=5627"/>
		<updated>2010-12-15T20:06:19Z</updated>

		<summary type="html">&lt;p&gt;Getify: more response regarding multiple loading queues&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Please discuss the proposal here. All informal feedback is welcomed, and when it&#039;s appropriate, please add content to the main page.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
== What about CommonJS / RequireJS? ==&lt;br /&gt;
&lt;br /&gt;
I&#039;m not sure we need to complicate the HTML or browser engines in order to satisfy the advanced use cases.  To me it seems that the JavaScript langauge itself could abstract the requirements and let the browser vendors implement whatever solution they see fit.  Specifically, if JavaScript provided the define() and require() calls proposed in the CommonJS &amp;quot;specifications&amp;quot; (and implemented by [http://requirejs.org/docs/api.html RequireJS], [http://svn.dojotoolkit.org/src/dojo/trunk/_base/_loader/loader.js dojo (trunk)], and others *Yabble?), then browsers could just load and evaluate scripts as fast as possible without regard to script execution order.  &lt;br /&gt;
&lt;br /&gt;
This would solve all of the problems except for the current inability to use loaders like LABjs in FF4 or webkit.  To solve that problem, we should consider keeping the existing functionality or triggering the deprecated functionality when needed.&lt;br /&gt;
&lt;br /&gt;
--[[User:Unscriptable|Unscriptable]] 01:29, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
=== Response ===&lt;br /&gt;
&lt;br /&gt;
This is a valid point except that it assumes that all code that needs to be loaded is &amp;quot;modular&amp;quot;... in fact, most code on the internet that needs to be loaded is NOT modular, and instead requires particular ordering when being loaded... for instance, &amp;quot;jquery&amp;quot; and &amp;quot;jquery-ui&amp;quot; -- jQuery must be run before jQuery-UI is loaded and run.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The spirit of this proposal is to add the facility to native browser (rendering engine, specifically) behavior, such that any and all existing content is able to be loaded (even with dependencies and order) without modification.&lt;br /&gt;
&amp;lt;blockquote style=background-color:#eee&amp;gt;&lt;br /&gt;
Are we trying to codify how one would implement this HTML dynamically so I can achieve parallel loading but retain dependency that is inferred in the order? &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: white&amp;quot;&amp;gt; &lt;br /&gt;
&amp;amp;lt;script src=&amp;quot;jquery.js&amp;quot;&amp;amp;gt; &amp;amp;lt;/script&amp;amp;gt; &amp;lt;br/&amp;gt; &lt;br /&gt;
&amp;amp;lt;script src=&amp;quot;jquery-ui.js&amp;quot;&amp;amp;gt; &amp;amp;lt;/script&amp;amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
If so, we must consider the case in which the dependency itself loads additional code which the discussions around `async=false` or even text/cache do not account for.  &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:23, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Dependent code should be modular ===&lt;br /&gt;
&lt;br /&gt;
I completely agree, 99% of JavaScript in use today is NOT modular, but that&#039;s arguably because there was no built-in support to make it modular (IMHO, YMMV, RTFM, BBQ, ROFL).  But that doesn&#039;t mean there aren&#039;t dependencies that span script files / script nodes.  I don&#039;t know how many sites would be broken (or if there is any way to determine how many sites would be broken) if/when the latest browsers started loading and evaluating script-injected scripts out of order.  It&#039;s not just LABjs-based sites.  I&#039;ll bet many dojo and YUI sites will break, too (although I don&#039;t know this for a fact because I don&#039;t know exactly how their dynamic script loaders work).  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;In any case, breaking existing sites is a BAD IDEA. :)  There should be some way to allow LABjs and other dynamic script loaders to continue to work.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
The part I don&#039;t agree with in the proposals is that it should be the job of HTML and/or the browser engine to manage dependencies.  Specifically, I am talking about the scriptGroup element and the &amp;quot;waitFor&amp;quot; attribute.  The &amp;quot;async&amp;quot; attribute may be the right level: it appropriates only general heuristics to the browser: &#039;&#039;load these scripts whenever you like, but load these other scripts in this order&#039;&#039;.  &lt;br /&gt;
&lt;br /&gt;
Detailed dependency management belongs in the modules, though.  This is how it&#039;s done in most environments / languages: import statements, include statements, etc.  It&#039;s no different in JavaScript, if a developer wants to take advantage of some code in another module, she/he should import it into their current module.  This should be the model moving forward.  It&#039;s not hard, it&#039;s not un-web-like, and it&#039;s already being done.  &lt;br /&gt;
&lt;br /&gt;
Existing code can be made to work modularly, too:&lt;br /&gt;
&lt;br /&gt;
1. Identify modules and wrap them with a define() method.&lt;br /&gt;
2. Identify dependencies using either the [http://wiki.commonjs.org/wiki/Modules/AsynchronousDefinition Asynchronous Module Definition] format or the CommonJS synchronous format&lt;br /&gt;
&lt;br /&gt;
async format:&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
define([&#039;dependency1&#039;, &#039;dependency2&#039;], function (dependency1, dependency2) {&lt;br /&gt;
	/* this is the factory function, return the module inside here */&lt;br /&gt;
	return { /* some object, but could also be a constructor or a function */ };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
sync format:&lt;br /&gt;
&amp;lt;pre lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
define(function (require, exports, module) {&lt;br /&gt;
	/* this is the factory function, require dependencies and return the module inside here */&lt;br /&gt;
	var dependency1 = require(&#039;dependency1&#039;);&lt;br /&gt;
	var dependency2 = require(&#039;dependency2&#039;);&lt;br /&gt;
	return { /* some object, but could also be a constructor or a function */ };&lt;br /&gt;
});&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
In the async case, the browser may employ whatever method it sees fit (e.g. parallel) to download the dependencies since the code inside the factory function won&#039;t execute until all of the dependencies are loaded and evaluated.  In the sync case, the browser has no choice but to download and evaluate the dependency before continuing code execution.  It&#039;s conceivable that the browser could find and asynchronously pre-fetch the inline require()&#039;ed dependencies in the sync case.  (RequireJS doesn&#039;t quite do this.  Instead I believe it finds the inline require() calls and converts the module to an async module.)  &lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;&#039;&#039;So, in short: don&#039;t break LABjs (or any other script loaders), but don&#039;t extend the spec to manage dependency management from HTML.&#039;&#039;&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
--[[User:Unscriptable|Unscriptable]] 01:29, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Response #2 ==&lt;br /&gt;
&lt;br /&gt;
The cat&#039;s already out of the bag... we&#039;ve got a dozen+ years worth, hundreds of millions of javascript files out there, which are NOT modular, and will not be modified to be modular. &lt;br /&gt;
&lt;br /&gt;
If we&#039;re talking about either trying to change all the JS on the web to be modular, or trying to extend the HTML markup spec to include a better facility for accomodating the existing script content, I think the former is idealistic and &amp;quot;correct&amp;quot; but the latter is realistic and &amp;quot;right&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
A modular loading system will only work for modular code, but a general loading mechanism in HTML (like the &amp;amp;lt;script&amp;gt; tag) can load both modular and non-modular code, which is why that more general mechanism is what&#039;s in HTML right now and not some more specialized loading API that only loads a small percentage of JavaScript code out there.&lt;br /&gt;
&lt;br /&gt;
The &amp;amp;lt;script&amp;gt; tag has worked fine forever, and there&#039;s been no real big push to do away with it in favor of having all loading handled by JavaScript -- in fact, that would be impossible because if you had no way to load JS onto the page with a &amp;amp;lt;script&amp;gt; tag, then no JavaScript could run on a page to load more code. So I don&#039;t agree that we should stop adding functionality to the HTML loading mechanism and only focus on JavaScript.&lt;br /&gt;
&lt;br /&gt;
If we agree that the &amp;amp;lt;script&amp;gt; tag itself is not only required but normative, then I&#039;m not sure why decorating the &amp;amp;lt;script&amp;gt; tag with additional behavior to help it be more powerful is a bad pattern. We&#039;ve addded &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; already. I&#039;m not sure I understand why adding a wrapper tag called &amp;quot;scriptGroup&amp;quot; would be that much different than attributes on the &amp;amp;lt;script&amp;gt; tag?&lt;br /&gt;
&lt;br /&gt;
I also don&#039;t think that &amp;quot;scriptGroup&amp;quot; constitutes &amp;quot;dependency management&amp;quot;. &amp;quot;scriptGroup&amp;quot; is a semantic way for a web author to express that they want to group a set of scripts together ONLY so far as execution order behavior is concerned. It implies nothing more to it (that &amp;quot;dependency management&amp;quot; would imply) than that.&lt;br /&gt;
&lt;br /&gt;
For instance, consider this: if JavaScript had been built with non-global context and statement level context and sandboxing and such things, we&#039;d have a much more secure JavaScript right now. But just because JavaScript may someday be taught some new tricks in terms of security, that doesn&#039;t mean that &amp;quot;now&amp;quot; HTML shouldn&#039;t try to help solve the problem. If for instance the &amp;amp;lt;frag&amp;gt; tag can be used to create a sandboxed environment for third-party JavaScript code to run in, this is a much easier way to address the security problem than to suggest we have to wait for ES-Harmony to add extra syntax to the language to accomplish the same thing.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 02:13, 2 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== Text/cache solution ==&lt;br /&gt;
&lt;br /&gt;
It seems to me that having a real text/cache script type, or a similar element that fires a load/error event and provides a .text property gets you where you need to be. You could setAttribute(&amp;quot;type&amp;quot;, &amp;quot;text/JavaScript&amp;quot;) or use the text to create a new script element. Or a new style element, or whatever. That puts you in control of when script or CSS is interpreted. With async as the default, you don&#039;t need a new ordered attribute as that&#039;s easily managed in script.&lt;br /&gt;
&lt;br /&gt;
== &amp;quot;text/cache&amp;quot; response ==&lt;br /&gt;
&lt;br /&gt;
On the surface, this idea sounds like it&#039;s pretty similar to &amp;amp;lt;link rel=prefetch&amp;gt; -- in other words, we&#039;ve already standardized a way to prefetch a resource into cache, and probably in a better way than the hack that I did in LABjs of just relying on it fetching unrecognizable content.&lt;br /&gt;
&lt;br /&gt;
Unfortunately, it still suffers some similar problems to &amp;amp;lt;link rel=prefetch&amp;gt;. &lt;br /&gt;
&lt;br /&gt;
Firstly, if the notion is that you&#039;d use &amp;amp;lt;script type=&amp;quot;text/cache&amp;quot;&amp;gt; to prefetch, and then you&#039;d make a second request for the resource with a real &amp;amp;lt;script type=&amp;quot;text/javascript&amp;gt; node, it suffers the faulty assumption that the resource was served with proper cache headers.&lt;br /&gt;
&lt;br /&gt;
If we try to avoid the need for the second request by suggesting that you can access the `text` property of the node (and then eval() or inject that code when you want to execute it), this runs afoul of the same-origin policy problem if you requested the script from a non-local domain.&lt;br /&gt;
&lt;br /&gt;
The part of your suggestion that holds water would be if, given a &amp;quot;prefetched&amp;quot; script node that didn&#039;t execute yet, I could simply change that node&#039;s `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;, and that would explicitly trigger the browser to immediately execute it. &lt;br /&gt;
&lt;br /&gt;
This is an interesting idea. I can see though that it perhaps has some potential complications. &lt;br /&gt;
&lt;br /&gt;
For instance, would the browser have to keep a &amp;quot;hasExecuted&amp;quot; internal flag, so that changing the `type` value would only at most result in one execution? Also, if the script node in question is loaded but not executed, can the page alter the script code (by setting its `text` property) before making the change to the `type` value? In that case, should the browser execute the original code or the modified code? I can see security implications and confusion abounding in that behavior.&lt;br /&gt;
&lt;br /&gt;
Lastly, as mentioned above, this basically copies (in a similar but not exactly identical way) the &amp;amp;lt;link rel=prefetch&amp;gt; behavior. It may be seen as a negative to be trying to step on the toes of &amp;amp;lt;link rel=prefetch&amp;gt;, and also it may be more confusing to the general public as to why you&#039;d use one technique or the other, given their differences being so nuanced.&lt;br /&gt;
&lt;br /&gt;
That having been said, I can see that it&#039;s a viable alternate proposal. It&#039;s worth adding a note to that section of the main Wiki page and to getting some feedback from browser vendors on if they feel this is a more realistic/suitable solution to the use-case.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #eee&amp;quot;&amp;gt;&lt;br /&gt;
The difference between pre-fetching and this proposal seems pretty clear: Prefetching, if not performed now, will eventually be done once the user explicitly requests the page. while the &amp;quot;text/cache&amp;quot; solution indicates that the resource should load &#039;&#039;&#039;immediately&#039;&#039;&#039;, not during idle time. &lt;br /&gt;
&lt;br /&gt;
&amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:10, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 20:56, 4 November 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
== RequireJS order support ==&lt;br /&gt;
&lt;br /&gt;
I am the main developer for RequireJS, and just want to voice my support for a solution that works on the script element itself, either by leveraging the async attribute or allowing something like an &amp;quot;order&amp;quot; boolean attribute that would allow specifying a dynamically added script element should be executed in the order that it appears in the DOM. This capability should be something can be capability detected to avoid browser sniffing.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
Why shouldn&#039;t dependency management be implemented in high-level code and the browser simply provide a means by which parallel loading can be achieved?  As you know, there is a considerable amount of nuance.  I&#039;m of the opinion that implementing dependency management in the engine will eventually lead us right back to where we were 10 years ago with inline script tags which provides an easy method for dependency management at the expense of unmanaged blocking and an inability to detect or adapt to errors. &amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:51, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
There are many scripts on the web today that have implicit dependencies that should be loaded in a specific order, but the application developer will want to use dynamically created script elements for best performance. jQuery plugins that depend on the jQuery script already been executed in the page are a good example.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#eee&amp;quot;&amp;gt;&lt;br /&gt;
Agreed, but I don&#039;t believe the proposals allow for proper emulation of the dependency management provided by script tags.  Prototype.js, for example, loads its core modules dynamically (using document.write so maybe not the best example...).  When prototype.js is included via script tags, ensuing script blocks are free to &amp;quot;depend&amp;quot; on all of these modules; Dynamically creating a dependency on prototype.js by loading both with the `async=true` property will fail since prototype.js won&#039;t be able to &amp;quot;jump the queue&amp;quot; and add insure its core modules load before its extensions.  &amp;lt;br/&amp;gt; &lt;br /&gt;
[[User:Serverherder|Serverherder]] 19:51, 15 December 2010 (UTC)&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ccc&amp;quot;&amp;gt;&lt;br /&gt;
I have to admit, you raise a valid point with respect to that common usage. The YUI model (where the framework is responsible for all loading) is perhaps the more appropriate model to move toward, though. Or, the framework doesn&#039;t auto-load its own dependencies at all. In either case, I think it&#039;s cleaner to have one loading mechanism in play rather than two or more competing ones, because as you point out, this is going to get messy. I still am not sure this means that the async=false proposal is lacking, because as I think about how complicated such a split loading would be with your `readyState` proposal, that seems even worse/more complicated to me. [[User:Getify|Getify]] 20:06, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
- James Burke&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5619</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5619"/>
		<updated>2010-12-15T18:38:41Z</updated>

		<summary type="html">&lt;p&gt;Getify: added note about relation between two sections&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question. This alternate proposal is very similar in spirit to the [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|previous section on explicit support for &amp;quot;text/cache&amp;quot;]] mime-type value.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5618</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5618"/>
		<updated>2010-12-15T18:35:29Z</updated>

		<summary type="html">&lt;p&gt;Getify: response to IE and `readyState` viability comment&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
When loading a script that is in the cache, IE requires the event handler be installed prior to setting the `src` attribute.  Otherwise the element&#039;s transition to the &amp;quot;loaded&amp;quot; state will be missed.  IE routinely &amp;quot;skips&amp;quot; ready state transitions when cached scripts are immediately inserted into the DOM so suspicion is understandable; however, delayed insertion works around this &amp;quot;problem&amp;quot;.  The script above has been tested in IE6-8 with scripts in a variety of caching states and found to work correctly. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I think you&#039;re right: Only IE impelements this event and property on script elements.  The readystatechange event, however, is widely understood due to the XmlHttpRequest implementation and `readyState` property has been added to the `document` in WebKit, Mozilla and IE. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color:#ddd;&amp;quot;&amp;gt;&lt;br /&gt;
Also, the suggestion of handling the &amp;quot;preload&amp;quot; and deferred execution via the script tag&#039;s `readyState` property is very similar in spirit to the suggestion that was made to explicitly support [[Dynamic_Script_Execution_Order#Explicit_support_for_.22text.2Fcache.22_MIME_type_behavior|preloading into cache without execution]]. As I said, either of those two approaches might work, but they both put more of the onus of complexity in managing multiple dependencies on the high-level code (author, script loader, etc). I&#039;m more in favor of this functionality being explicitly built-in natively into the queues. As always, I welcome comments to any side of this discussion.&lt;br /&gt;
&lt;br /&gt;
[[User:Getify|Getify]] 18:35, 15 December 2010 (UTC)&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5616</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5616"/>
		<updated>2010-12-15T18:10:00Z</updated>

		<summary type="html">&lt;p&gt;Getify: response to high-level code comment&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EFEFEF&amp;quot;&amp;gt;&lt;br /&gt;
The scriptgroup or `readyState`  alternate proposals allow for dependency management to be done in high-level code.  The singular syncrhonized execution queue proposed here introduces artificial dependencies, undermining some of the benefits dynamic script loading provides.  &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc;&amp;quot;&amp;gt;&lt;br /&gt;
It&#039;s important to note that `async=false` scripts will load in a separate queue from `async=true` scripts. It&#039;s not that all scripts will now be forced into this insertion-order-execution queue. An author (or script loader) can chose which queue to put a script loading into based on the needs of the page and the resource. If I have a few scripts that are unrelated to each other and the main page (like Google Analytics, social sharing buttons, etc), I&#039;ll load those via `async=true`. For other scripts, where order matters for dependency sake, I&#039;ll use `async=false`. I think having both those queues available to high-level code is enough control over loading to serve the majority of use-cases pretty well.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5614</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5614"/>
		<updated>2010-12-14T22:17:29Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding &amp;quot;readyState preloading&amp;quot; alternate proposal&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
==== readyState &amp;quot;preloading&amp;quot; ====&lt;br /&gt;
Per the suggestion/comment added to the above [[Dynamic_Script_Execution_Order#Current_Limitations|Current Limitations]] section, it&#039;s been suggested that IE&#039;s current behavior with respect to the `readyState` property can adequately serve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
The idea is that IE will begin fetching into the cache a script resource as soon as the `src` attribute of the dynamic script element is set. But IE will not execute the script (even if it finishes loading) until the script element is added to the DOM. However, the `readyState` property of the element, combined with the `onreadystatechange` handler listening for changes, can detect when the script has finished loading, by receiving the &amp;quot;loaded&amp;quot; value.&lt;br /&gt;
&lt;br /&gt;
In this way, a script loader could &amp;quot;preload&amp;quot; a set of scripts all in parallel, but control their execution order by delaying the appending of the element to the DOM until execution is desired.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5613</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5613"/>
		<updated>2010-12-14T22:09:19Z</updated>

		<summary type="html">&lt;p&gt;Getify: response to comments&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
I&#039;m not sure if this is the case.  &lt;br /&gt;
&lt;br /&gt;
IE will start fetching resources without execution simply by setting the src attribute.  Scripts will not execute, however, until they are added to the document.  Since IE supports the readyState attribute and readystatechange event, it&#039;s possible to load scripts in parallel and execute them in order without resorting to invalid type attribute hack-ery.  &lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre style=&amp;quot;background-color:white&amp;quot; lang=&amp;quot;javascript&amp;quot;&amp;gt;&lt;br /&gt;
script= document.createElement(&amp;quot;SCRIPT&amp;quot;); &lt;br /&gt;
script.onreadystatechange= function(){ &lt;br /&gt;
    if(this.readyState == &#039;loaded&#039;)&lt;br /&gt;
          /* &lt;br /&gt;
             Network fetch is now complete, following&lt;br /&gt;
             triggers synchronous execution.  &lt;br /&gt;
          */&lt;br /&gt;
          document.body.appendChild(this); &lt;br /&gt;
    else if(this.readyState == &amp;quot;complete&amp;quot;)&lt;br /&gt;
          this.onreadystatechange= null; &lt;br /&gt;
}&lt;br /&gt;
script.src= &amp;quot;foo.js&amp;quot;; &lt;br /&gt;
/* Network fetching begins now */&lt;br /&gt;
&amp;lt;/pre&amp;gt; &lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #ccc&amp;quot;&amp;gt;&lt;br /&gt;
I&#039;ve experimented with such things heavily in the past, and I&#039;m fairly suspicious that this is not reliable across various versions of IE (6-9), nor does it work reliably the same depending on if an element is already in the browser cache or not. I&#039;m not 100% certain that what you suggest is flawed, but I have a strong hunch that it can be shown to have some holes in it for IE loading. I will try to create some test cases to prove or disprove.&lt;br /&gt;
&lt;br /&gt;
Moreover, you&#039;re suggesting something that is AFAIK only IE right now, and definitely appears to not be standardized. In the same way that I&#039;m hesitant to continue pinning loading behavior on hacks like the invalid type attribute value, I wouldn&#039;t want to approach loading with this technique (even if it were proven reliable on all IE&#039;s under all caching circumstances) unless it was a serious proposal to the W3C to have this technique be the new standard (as opposed to the current proposal, as implemented by Mozilla and Webkit now).&lt;br /&gt;
&lt;br /&gt;
That having been said, I think this is at least a viable candidate for &amp;quot;Alternate Proposals&amp;quot; and deserves to be listed as such. As always, I welcome input from the community on all the ideas presented here, including this one. But, since the main proposal (async=false) is already being implemented by at least two major browsers, I&#039;m not sure if the tide will be changed or not.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in &amp;lt;s&amp;gt;IE and &amp;lt;/s&amp;gt;Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Gecko&#039;s strict execution order creates dependencies between scripts which are unrelated and prevents the ability mitigate the impact of server outages / network latency.  This is exacerbated by the fact that Gecko, unlike IE and WebKit, provides no mechanism to &amp;lt;b&amp;gt;remove&amp;lt;/b&amp;gt; scripts from the execution queue. &lt;br /&gt;
&lt;br /&gt;
Consider the case where JSONP is used to provide auto-completion.  The user types &amp;quot;hello&amp;quot; and the JSONP request is made; however, the &amp;quot;currently&amp;quot; loading script is out-of-date as soon as the user modifies his search to &amp;quot;hello world&amp;quot;.  Gecko&#039;s algorithm requires the out-of-date request load and execute before the second request.&lt;br /&gt;
&amp;lt;/blockquote&amp;gt; &lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;blockquote style=&amp;quot;background-color: #EEEEEE&amp;quot;&amp;gt; &lt;br /&gt;
Considering Internet Explorer&#039;s readyState implementation provides a means of both parallel loading and preserving execution order can be achieved, perhaps its &amp;quot;readyState&amp;quot; property and onreadystatechange event should be included. &lt;br /&gt;
&amp;lt;/blockquote&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5597</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5597"/>
		<updated>2010-11-29T15:40:41Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* Proposal Amendment: Events */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in IE and Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;load&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5596</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5596"/>
		<updated>2010-11-26T20:15:13Z</updated>

		<summary type="html">&lt;p&gt;Getify: /* External Discussions of this Feature */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in IE and Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://www.w3.org/Bugs/Public/show_bug.cgi?id=11295 W3C Bug #11295]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugzilla.mozilla.org/show_bug.cgi?id=602838 Mozilla Bug #602838]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[https://bugs.webkit.org/show_bug.cgi?id=50115 Webkit Bug #50115]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://hsivonen.iki.fi/script-execution/ HTML5 Script Execution Changes in Firefox 4 Beta 7]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model Details (without the event amendment) ====&lt;br /&gt;
&lt;br /&gt;
When a script element node is created, if it is being flagged as parser-inserted, set its force-async flag to false. Otherwise, set its force-async flag to true. (Note that createContextualFragment, innerHTML and XSLTProcessor::transformToFragment-created scripts are not flagged as parser-inserted.) This flag setting happens before any attributes (even parser-set ones) are set on the node.&lt;br /&gt;
&lt;br /&gt;
When a previously-created script element node loses its parser-insertedness, if the element doesn&#039;t have the async content attribute, set the force-async flag to true and false otherwise.&lt;br /&gt;
&lt;br /&gt;
When a script element node obtains the async content attribute (via setAttribute, setAttributeNode, setAttributeNS, by the fragment parser or the XSLTProcessor adding the attribute, etc.), set the force-async flag to false. (Note that calling removeAttribute(&amp;quot;async&amp;quot;) doesn&#039;t modify the force-async flag.)&lt;br /&gt;
&lt;br /&gt;
The async IDL attribute must behave as follows:&lt;br /&gt;
* Upon setting, set the force-async flag to false and then reflect the async content attribute.&lt;br /&gt;
* Upon getting, if the force-async flag is true, return true. Otherwise, reflect the async content attribute.&lt;br /&gt;
&lt;br /&gt;
In step 13. of http://www.whatwg.org/specs/web-apps/current-work/#running-a-script before the case &amp;quot;If the element has a src attribute&amp;quot; add a case:&lt;br /&gt;
If the script has a src attribute and the async IDL property getter returns false,&lt;br /&gt;
The element must be added to the queue of ordered script-inserted external scripts of the Document of the script element at the time the running a script algorithm started.&lt;br /&gt;
&lt;br /&gt;
The task that the networking task source places on the task queue once the fetching algorithm has completed must run these steps:&lt;br /&gt;
# If the queue of ordered script-inserted external scripts is empty or the first script in the queue of ordered script-inserted external scripts has not been fetched yet, abort these steps.&lt;br /&gt;
# Execute the first script in the queue of ordered script-inserted external scripts.&lt;br /&gt;
# Remove the first script from queue of ordered script-inserted external scripts.&lt;br /&gt;
# Goto step #1.&lt;br /&gt;
&lt;br /&gt;
Modify step 5 of  http://www.whatwg.org/specs/web-apps/current-work/#the-end to say:&lt;br /&gt;
Spin the event loop until the set of scripts that will execute as soon as possible is empty and the queue of ordered script-inserted external scripts is empty.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5582</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5582"/>
		<updated>2010-11-04T21:09:09Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding link to the discussion thread&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
Please feel free to join the discussion of this topic in the [[Talk:Dynamic_Script_Execution_Order]] area.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in IE and Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
	<entry>
		<id>https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5581</id>
		<title>Dynamic Script Execution Order</title>
		<link rel="alternate" type="text/html" href="https://wiki.whatwg.org/index.php?title=Dynamic_Script_Execution_Order&amp;diff=5581"/>
		<updated>2010-11-04T21:07:18Z</updated>

		<summary type="html">&lt;p&gt;Getify: adding &amp;quot;text/cache&amp;quot; alternate proposal&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;This page is intended to formalize the discussion around an important but currently underserved use-case (in both spec and various browser implementations): the need for a dynamic script loading facility that can download resources in parallel but ensure that they execute serially in insertion order, for dependency reasons.&lt;br /&gt;
&lt;br /&gt;
A long email thread on the W3C public-html list, which began [http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html here] and for which a recent message is [http://lists.w3.org/Archives/Public/public-html/2010Oct/0415.html here], has been discussing this problem, but the email thread is becoming unruly and hard to track, so this wiki page will now be the official location to discuss the topic.&lt;br /&gt;
&lt;br /&gt;
Briefly, this issue has arisen because recent &amp;quot;nightlies&amp;quot; changes in both Mozilla Gecko and Webkit have broken the ability for script-loaders like [http://labjs.com LABjs] to be able to download scripts in parallel but ensure their execution order. As a result of the discussion, it&#039;s become apparent that both the spec for, and various current browser implementations around, dynamic script loading is incomplete in addressing this use case, and that some change needs to occur.&lt;br /&gt;
&lt;br /&gt;
There is one main proposal that has surfaced from the discussions, with several other alternatives having been discussed. This page will try to distill the long email thread down and clearly present the main proposal, objections, and alternative suggestions.&lt;br /&gt;
&lt;br /&gt;
== Use Case Description ==&lt;br /&gt;
Script tags/elements can either: &lt;br /&gt;
&lt;br /&gt;
a) appear in the HTML markup (&amp;quot;parser-inserted&amp;quot;), OR&lt;br /&gt;
&lt;br /&gt;
b) be dynamically appended to the DOM using document.createElement() (&amp;quot;script-inserted&amp;quot;)&lt;br /&gt;
&lt;br /&gt;
Parser-inserted script tags, up until recent browser versions, had the undesirable performance behavior of loading and executing serially, blocking everything else while doing so. Recently, many browsers have improved the situation by loading the scripts in parallel, but still executing them in order.&lt;br /&gt;
&lt;br /&gt;
On-demand (or dynamic) script loading has emerged in recent years for a variety of different reasons, most notably the performance improvements to address such concerns. It is desired by many different scenarios to be able to download scripts to a page completely independently of the loading of the rest of a page&#039;s resources, or even well after a page has finished loading (&amp;quot;on-demand&amp;quot;).  &lt;br /&gt;
&lt;br /&gt;
Recent additions to HTML spec such as &amp;quot;defer&amp;quot; and &amp;quot;async&amp;quot; were intended to address the use-case for parser-inserted script elements, but their behaviors have been unhelpful for the on-demand loading use-case (script-inserted script elements).&lt;br /&gt;
&lt;br /&gt;
Thus, script loaders (like [http://labjs.com LABjs]) were developed to give page authors an easy way to specify one or more scripts to load (regardless of when in the life-time of the page that loading is requested), and for as many of them as possible to load in parallel, and for those scripts to be loadable from any local or remote domain location, and for the script loader to be able to control the execution order (if the usage of the script loader&#039;s API expresses the need to) to preserve dependencies between the scripts.&lt;br /&gt;
&lt;br /&gt;
Sometimes, dynamic script loading is used to load totally independent scripts, and thus &amp;quot;as fast as possible&amp;quot; execution is desired. Other times (and possibly more frequently), multiple scripts are loaded with some  dependencies among them, requiring them to execute in a certain order. &lt;br /&gt;
&lt;br /&gt;
What is needed is some facility by which a script loader can express that dynamic script loading either does or does not need execution order preserved among the queue of requested script loadings.&lt;br /&gt;
&lt;br /&gt;
=== Current Limitations ===&lt;br /&gt;
Unfortunately, browser behavior around script-inserted script elements and their loading and execution behavior is splintered. There are, at present, two main camps of behavior:&lt;br /&gt;
&lt;br /&gt;
a) in IE and Webkit (including Chrome), the default behavior for script-inserted script elements is for them all to execute in &amp;quot;as fast as possible&amp;quot; mode, meaning there&#039;s no guarantee about ordering. This effectively makes on-demand (dynamic) script loading impossible to work in parallel-mode if the resources in question have dependencies -- the only straight-forward way to handle things is to load each file and execute, serially, losing the parallel loading performance benefits.&lt;br /&gt;
&lt;br /&gt;
b) in Gecko and Opera, the default behavior for script-inserted script elements is for them all to load in parallel, but execute serially in insertion order. Technically, this makes it impossible to dynamically load independent script elements and have them execute in &amp;quot;as fast as possible&amp;quot; mode.&lt;br /&gt;
&lt;br /&gt;
As described above, both behaviors are desirable under different circumstances, but each of the two camps provides only one behavior, and no way to straightforwardly achieve the other. This obviously creates a big nightmare interoperability-wise when trying to provide a general script loader cross-browser.&lt;br /&gt;
&lt;br /&gt;
Since it is observed that the behavior in (a) is more detrimental (race-condition wise) for the case where dependencies exist, script loaders like LABjs had to find some way around this problem while still attempting the best possible parallel loading performance. However, the trick used is hacky and not completely reliable -- yet it is the best way to solve the use-case in IE and Webkit. &lt;br /&gt;
&lt;br /&gt;
For Gecko/Opera, the concession was just (silently) made that the lesser-common use case of &amp;quot;as fast as possible&amp;quot; wasn&#039;t possible, but degraded fine to &amp;quot;insertion order execution&amp;quot;, while keeping the parallel loading benefits.&lt;br /&gt;
&lt;br /&gt;
Recently, Gecko landed a [http://hg.mozilla.org/mozilla-central/rev/a60414d076b5 patch] to the trunk that stopped Gecko&#039;s existing behavior of preserving execution order, making script-inserted script elements now execute in &amp;quot;as fast as possible&amp;quot; mode similiar to IE/Webkit. Unfortunately, the &amp;quot;workaround&amp;quot; used in IE/Webkit (described in the next section) for dealing with parallel loading and execution-order dependencies does not work in Gecko, which means presently the use-case in question is now broken in Gecko trunk/FF4 nightlies.&lt;br /&gt;
&lt;br /&gt;
Moreover, Webkit recently landed a [http://trac.webkit.org/changeset/67245 patch] to the trunk that stopped Webkit&#039;s non-standard but long-held behavior (also used in the &amp;quot;workaround&amp;quot; described in the next section) of loading into cache script resources with an unrecognized &amp;quot;type&amp;quot; value, but silently not executing them. This behvaior (while hacky) is central to being able to address the use-case in question in Webkit, so at present, Webkit nightlies are now also entirely broken on the use-case (though in a different way than Gecko).&lt;br /&gt;
&lt;br /&gt;
Both the Gecko change and the Webkit change are well-intentioned, as they are bringing the respective browser more in line with the HTML spec. However, what&#039;s really been demonstrated is that the HTML spec is not properly handling this use-case, and so the goal is not to address the browser issues raised with more awkward hacks, but to address the shortcomings of the spec first, and then encourage all browsers to adhere to such.&lt;br /&gt;
&lt;br /&gt;
=== Current Usage and Workarounds ===&lt;br /&gt;
To work around the limitation in IE/Webkit(prior to the above noted patch) of not being able to rely on script order execution for parallel loaded scripts, a &amp;quot;preloading&amp;quot; trick was developed. This trick relied on non-standard (but long-held) behavior in these browsers that a script-inserted script element with an unrecognized &amp;quot;type&amp;quot; value (such as &amp;quot;script/cache&amp;quot;) would be fetched/loaded, but would not execute. This had the effect of loading the resource into cache, and then firing the &amp;quot;load&amp;quot; handlers to let the page know when the resource was completely in cache. &lt;br /&gt;
&lt;br /&gt;
Assuming that the resource was served with proper caching headers, and was in fact in the cache, it could then be executed (nearly) immediately when it was the proper execution order time by re-requesting the same resource via another script-inserted script element with the proper &amp;quot;text/javascript&amp;quot; type value, pulling the resource from the cache and executing it, without another server round-trip.&lt;br /&gt;
&lt;br /&gt;
Of course, the assumption of proper cache headers is a huge one, and not at all reliable. Some recent estimates by performance optimization specialists have suggested as much as 70% of scripts across the internet are not served with proper caching headers, which means that such scripts would be completely ineffective if loaded using this (or a similar) technique. The script resource would end up being loaded completely a second-time, and the &amp;quot;near immediate&amp;quot; execution would obviously be false, and thus race conditions would ensue.&lt;br /&gt;
&lt;br /&gt;
It&#039;s important to note at this point that the new &amp;amp;lt;link rel=prefetch&amp;gt; facility has been suggested as a better workaround, but it suffers the same ill-fated assumption of script cacheability. Still others have suggested &amp;quot;new Image().src=...&amp;quot; or the [http://www.phpied.com/preload-then-execute/ &amp;amp;lt;object&amp;amp;gt; preloading trick] suggested by Stoyan Stefanov. Again, these tricks unwisely assume cacheability, for the &amp;quot;preloading&amp;quot; trick to solve the use-case in question.&lt;br /&gt;
&lt;br /&gt;
=== At Risk ===&lt;br /&gt;
Currently, there are several large/popular web sites which are either currently (or who intend to soon) use LABjs in such a way as to run afoul of the new Gecko and Webkit behavioral changes with LABjs failing to operate properly. It&#039;s important to note that the problems of race conditions can be subtle and hard to detect, and so merely loading up such sites and failing to observe overt failure is not sufficient.&lt;br /&gt;
&lt;br /&gt;
Sites which are known to have LABjs loading techniques in place with currently broken (or susceptible to such breakage in the near future) behavior are:&lt;br /&gt;
&lt;br /&gt;
* [http://twitter.com Twitter]&lt;br /&gt;
* [http://zappos.com Zappos]&lt;br /&gt;
* [http://vimeo.com Vimeo]&lt;br /&gt;
* [http://mefeedia.com MeFeedia]&lt;br /&gt;
* [http://blog.getify.com Getify Solutions blog (my blog)]&lt;br /&gt;
* [http://flensed.com flensed Flash+JavaScript browser shims (cross-domain Ajax, cross-browser cookies, etc)]&lt;br /&gt;
&lt;br /&gt;
Rather than getting hung up in the syntax of usage in LABjs that is or is not going to break, it&#039;s best to just think of the problem this way:&lt;br /&gt;
&lt;br /&gt;
Does a site need to load more than one script, at least one of which comes from a remote domain location (like a CDN), and for which among the scripts there&#039;s at least one execution-order dependency among them? If so, then that site is susceptible to the current/future breakage if the HTML spec (and browsers) do not address this use case.&lt;br /&gt;
&lt;br /&gt;
A common example (in use on many sites) of such might be loading:&lt;br /&gt;
&lt;br /&gt;
* jQuery from the CDN&lt;br /&gt;
* jQuery-UI from the CDN&lt;br /&gt;
* plugins and usage code in one or more local files&lt;br /&gt;
* Google Analytics from the Google domain&lt;br /&gt;
&lt;br /&gt;
Note that the emergence of popular script frameworks and their hosting on public CDN&#039;s is leading to more and more sites loading scripts from both local and remote locations, and also to loading more files that have dependencies (rather than the practice of concat&#039;ing all files into one file to avoid dependency issues).&lt;br /&gt;
&lt;br /&gt;
Any site which fits a profile like the above, and which might currently (many do), or in the future want to, use a script loader to improve their loading performance, will fail to achieve what they want cross-browser and in a performant way, if the HTML spec (and browsers) do not address the use case.&lt;br /&gt;
&lt;br /&gt;
=== Benefits ===&lt;br /&gt;
The benefits of addressing both behaviors directly (without &amp;quot;preloading&amp;quot; tricks and bad assumption reliance) have been implied in the above discussion, but in short are:&lt;br /&gt;
&lt;br /&gt;
a) clear and simplified code for script loaders, which leads to easier use by authors of more pages, which in turn leads to better web performance (as demonstrated clearly by intelligent script loading techniques as compared to just simple &amp;amp;lt;script&amp;gt; tags in HTML markup)&lt;br /&gt;
&lt;br /&gt;
b) full access to either/both of the execution-order behaviors (as the author sees fit), regardless of browser&lt;br /&gt;
&lt;br /&gt;
c) avoiding reliance on bad assumptions (like cacheability) as a sufficient way to address the use-case&lt;br /&gt;
&lt;br /&gt;
=== External Discussions of this Feature ===&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://lists.w3.org/Archives/Public/public-html/2010Oct/0088.html W3C public-html email thread]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/labjs-w3c-brief-update/ LABjs and W3C... brief update]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
* &amp;lt;cite&amp;gt;[http://blog.getify.com/2010/10/ff4-script-loaders-and-order-preservation/ FF4, script loaders, and order preservation]&amp;lt;/cite&amp;gt; &lt;br /&gt;
&lt;br /&gt;
== Proposed Solutions ==&lt;br /&gt;
&lt;br /&gt;
=== My Solution ===&lt;br /&gt;
The current proposal most well supported from the email discussion thread, and the one which I feel makes most sense, is described here.&lt;br /&gt;
&lt;br /&gt;
The HTML spec already defines the &amp;quot;async&amp;quot; attribute for parser-inserted script tags, which when set to &amp;quot;true&amp;quot;, changes their execution order behavior to be like script-inserted script-elements (in IE/Webkit), which is that they load in parallel and execute &amp;quot;as fast as possible&amp;quot; (ostensibly because the author is expressing no dependencies between multiple such &amp;quot;async&amp;quot;-marked scripts). Parser-inserted script elements without &amp;quot;async&amp;quot; (or with it set to false) behave as before and expected, which is that they load in parallel but execute in order.&lt;br /&gt;
&lt;br /&gt;
However, the HTML spec does not define the &amp;quot;async&amp;quot; property (or any such behavior) for script-inserted script nodes (such as those created by a script loader). Instead, the spec implies that &amp;quot;async=true&amp;quot; like behavior is always true for such script-inserted script elements.&lt;br /&gt;
&lt;br /&gt;
What is proposed is:&lt;br /&gt;
&lt;br /&gt;
a) Script-inserted script elements should have (and respect the value of) an &amp;quot;async&amp;quot; property which is basically identical to the &amp;quot;async&amp;quot; attribute for parser-inserted script elements. That is, script elements with the &amp;quot;async&amp;quot; property set to &amp;quot;true&amp;quot; will behave accordingly, as will script elements with the &amp;quot;async&amp;quot; property set to false. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is to mirror the &amp;quot;async&amp;quot; attribute behavior of parser-inserted script elements as an &amp;quot;async&amp;quot; property on script-inserted script elements. This has the benefit of using an existing facility and extending it (from current spec) in a way that is sensisble and symmetric with its current definition.&lt;br /&gt;
&lt;br /&gt;
b) Furthermore, to aid in &amp;quot;feature-detection&amp;quot; of such new behavior, the proposal is to have the default value for the &amp;quot;async&amp;quot; property of script-inserted script elements be &amp;quot;true&amp;quot; (and of course the associated behavior thereof).&lt;br /&gt;
&lt;br /&gt;
There are two major benefits to (b). One is that it provides a way to feature test such new behavior by not just looking for the existence of the &amp;quot;async&amp;quot; property on script elements, but specifically that the default value is &amp;quot;true&amp;quot; (which is opposite of what it would currently/normally be). Secondly, defaulting to &amp;quot;async=true&amp;quot; behavior for script-inserted script elements would preserve the default behavior of IE and Webkit, meaning there&#039;d be less of a chance of breaking existing web content in either of those two browsers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also important to note that there is no implied or requested effect or dependency between script-inserted script elements and parser-inserted script elements -- the two types of scripts would load and execute in entirely separate behavioral sandboxes.&lt;br /&gt;
&lt;br /&gt;
==== Proposal Amendment: Events ====&lt;br /&gt;
In addition to standardizing how scripts load and execute, it&#039;s also important to standardize the load and error events, and under what circumstances they fire, etc. Without reliable load/error events, the main proposal is not reliable and is thus unhelpful for the use-case.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after the resource finishes loading, but before parsing/execution, AND&lt;br /&gt;
&lt;br /&gt;
b) the script element loads any non-empty content (that is, it was a successful HTTP request), AND&lt;br /&gt;
&lt;br /&gt;
c) the script element has either loaded from the remote location, or is loaded from the brower cache&lt;br /&gt;
&lt;br /&gt;
Specifically, the script &amp;quot;load&amp;quot; event must come immediately between the script element finishing loading and the script element being parsed/executed. If the script resource successfully loads, nothing should interrupt the sequence of fininshing loading, the &amp;quot;load&amp;quot; event firing, and the script being parsed/executed.&lt;br /&gt;
&lt;br /&gt;
A script-inserted script element must fire the &amp;quot;error&amp;quot; event only once, when:&lt;br /&gt;
&lt;br /&gt;
a) immediately after a loading failure for the resource is detected (that is, an HTTP error received, such as 404, 500, 503, etc).&lt;br /&gt;
&lt;br /&gt;
Either the &amp;quot;load&amp;quot; or &amp;quot;error&amp;quot; event, but not both, will fire, and only once, for every script-inserted script element.&lt;br /&gt;
&lt;br /&gt;
NOTE: &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events on script-inserted script elements must fire synchronously to ensure event reliability.&lt;br /&gt;
&lt;br /&gt;
A case may be made for adding the &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events as described here to parser-inserted script elements as well.&lt;br /&gt;
&lt;br /&gt;
==== Processing Model ====&lt;br /&gt;
Script inserted script elements will have an &amp;quot;async&amp;quot; property that defaults to &amp;quot;true&amp;quot;. If the author does not change the value, all such requested script loadings will be in their own &amp;quot;queue&amp;quot; and will default to &amp;quot;as fast as possible&amp;quot; execution behavior. For any script elements that the author sets &amp;quot;async=false&amp;quot;, those scripts will load in a separate &amp;quot;queue&amp;quot;, and will execute in insertion order only. Again, these two &amp;quot;queues&amp;quot; will operate strictly independent of each other.&lt;br /&gt;
&lt;br /&gt;
==== Limitations ==== &lt;br /&gt;
The two behaviors being identified solve either the case where no dependencies exist, or where a linear dependency chain exists (and which the script elements can be requested to execute in that order). It is possible that some authors have a more complex non-linear dependency chain that they would like to express. This would obviously require a much more complex API and spec change, and since that use-case has not (yet) surfaced as a particularly main-stream request, I believe it would be overengineering to try to address it with this proposed change set.&lt;br /&gt;
&lt;br /&gt;
In addition, it&#039;s been duly noted that it&#039;s undesirable (and potentially confusing/problematic) to intentionally build in the inconsistency of having the &amp;quot;async&amp;quot; attribute (for parser-inserted scripts) and the &amp;quot;async&amp;quot; property (for script-inserted scripts) have different default values (&amp;quot;false&amp;quot; for the attribute, &amp;quot;true&amp;quot; for the property).&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been a point of discussion whether or not such a spec change has enough &amp;quot;carrot&amp;quot; to entice the browser vendors (namely IE and Webkit) to implement the behavior. Moreover, there&#039;s been some concern that if the default value for the &amp;quot;async&amp;quot; property were made to be &amp;quot;false&amp;quot; (like the attribute) to be more consistent and conservative, then it would possibly give the perception to IE and Webkit of &amp;quot;losing&amp;quot; some performance to cut out their default &amp;quot;as fast as possible&amp;quot; behavior.&lt;br /&gt;
&lt;br /&gt;
=== Alternate Proposals === &lt;br /&gt;
One early proposal on the email list was to introduce an entirely new property like &amp;quot;ordered&amp;quot; which an author could add to a script-inserted script element to instruct the browser to put it into the queue of execution-order-preserving script loadings. While such a property would address the use case, it introduces another property and thus more complicates the issue. It also fails to address the current spec inconsistency (which is confusing to new comers) that &amp;quot;async&amp;quot; is not a present/respected property in mirror of the attribute of the same name.&lt;br /&gt;
&lt;br /&gt;
Another suggestion was a &amp;quot;waitFor&amp;quot; property that would be added to script elements and would specify a list of one or more DOM id&#039;s of other script elements that the current script should &amp;quot;wait for&amp;quot; in terms of execution. Again, this would solve the use case, but in a more complicated way, and there are concerns that it would be too confusing for the normal use-case.&lt;br /&gt;
&lt;br /&gt;
Several suggestions have come in the form of creating explicit &amp;quot;preloading&amp;quot; (similar to &amp;amp;lt;link rel=prefetch&amp;gt;), but as described above, &amp;quot;preloading&amp;quot; to solve this use case is a non-ideal hack and highly susceptible to breakage if the script fails to be sent with proper caching headers.&lt;br /&gt;
&lt;br /&gt;
It&#039;s also been suggested that since this type of behavior is somewhat complicated, it may be better to intentionally obfuscate or complicate any such facility in the HTML, so as to make the barrier-to-entry rather high and force users to know what they are doing before doing it.&lt;br /&gt;
&lt;br /&gt;
It&#039;s been suggested that &amp;quot;defer&amp;quot; already preserves execution order. However, &amp;quot;defer&amp;quot; is only defined for parser-inserted scripts, and thus is not applicable to the use-case in question from an on-demand point of view. Also, &amp;quot;defer&amp;quot; scripts explicitly way for DOMContentLoaded, even if they&#039;re ready to execute sooner. So this is less than desired.&lt;br /&gt;
&lt;br /&gt;
Yet another proposal is a &amp;quot;document.executeScripts()&amp;quot; API, where an author can specify multiple sets of scripts that can load in parallel and it will enforce their execution order. A variation on that same idea was to use the &amp;quot;importScripts&amp;quot; from the Web Workers spec, however &amp;quot;importScripts&amp;quot; is synchronous (undesirable performance wise, obviously). The main downside (besides extra API complication) to &amp;quot;document.executeScripts()&amp;quot; is that there seem to be quite a few script execution properties/behaviors (including &amp;quot;document.currentScript&amp;quot; and charset override) which would have to be duplicated into this API facility.&lt;br /&gt;
&lt;br /&gt;
==== &amp;quot;Script Group&amp;quot; element ====&lt;br /&gt;
One recent alternate proposal bears some special consideration, as it seems like it might be a decent option (although certainly more of a radical change for the spec and for browsers to implement). But it has the appearance of being pretty straightforward and semantic for authors to use, perhaps even more so than using &amp;quot;async&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The proposal is to create a new HTML element, called perhaps &amp;amp;lt;scriptGroup&amp;gt;, &amp;amp;lt;collection&amp;gt;, etc. Specifically, this element must be able to be inserted wherever a &amp;amp;lt;script&amp;gt; element can currently be inserted. The &amp;quot;script group&amp;quot; element is intended to signify that all script elements added to it must perserve insertion execution order. This element wouldn&#039;t have much (but still some, explained in a moment) meaning in a parser-inserted context, since parser-inserted scripts already preserve order among themselves.&lt;br /&gt;
&lt;br /&gt;
An advantage of the &amp;quot;script group&amp;quot; element would be to give a direct and easy way to attach event listeners (&amp;quot;load&amp;quot; and &amp;quot;error&amp;quot;) to the entire group, rather than having to internally track events for each element if all you care about is the final &amp;quot;load&amp;quot; event, for instance. In the case of event handling, the &amp;quot;script group&amp;quot; element would have perhaps some benefit even in parser-inserted (markup) context.&lt;br /&gt;
&lt;br /&gt;
The element would need to have an &amp;quot;id&amp;quot; property, and possibly attributes for &amp;quot;load&amp;quot; and &amp;quot;error&amp;quot; events.&lt;br /&gt;
&lt;br /&gt;
A variation on how to look at this proposal is that a &amp;quot;script group&amp;quot; element could have an attribute/property on it (perhaps called &amp;quot;ordered&amp;quot;) which would allow the group to either be order preserved or not. This would make the &amp;quot;script group&amp;quot; element much more useful in the parser-inserted context, as it would sort of be a shortcut to setting &amp;quot;async=true&amp;quot; on all the child script elements.&lt;br /&gt;
&lt;br /&gt;
The &amp;quot;script group&amp;quot; element might look like this:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Or, with the &amp;quot;ordered&amp;quot; attribute to explicitly control ordering behavior for the group:&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group1&amp;quot; ordered=&amp;quot;true&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;foo.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;bar.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script&amp;gt;&lt;br /&gt;
      somethingInline();&lt;br /&gt;
    &amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
  &amp;amp;lt;scriptGroup id=&amp;quot;group2&amp;quot; ordered=&amp;quot;false&amp;quot; onload=&amp;quot;alldone();&amp;quot;&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;baz.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;far.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
    &amp;amp;lt;script src=&amp;quot;zab.js&amp;quot;&amp;gt;&amp;amp;lt;/script&amp;gt;&lt;br /&gt;
  &amp;amp;lt;/scriptGroup&amp;gt;&lt;br /&gt;
&lt;br /&gt;
While the &amp;quot;script group&amp;quot; element is definitely more complicated to define and implement, it does have some semantic advantages for authors, and it also would significantly reduce the internal complexity of script loaders like LABjs. It would give authors (either directly or through script loaders) the flexibility to group scripts together into one of the two aforementioned behaviors (execution &amp;quot;as fast as possible&amp;quot; or &amp;quot;in insertion order&amp;quot;), and to easily access both behaviors in the same page.&lt;br /&gt;
&lt;br /&gt;
==== Explicit support for &amp;quot;text/cache&amp;quot; MIME type behavior ====&lt;br /&gt;
Yet another alternate proposal has been suggested in the [[Talk:Dynamic_Script_Execution_Order]] thread. &lt;br /&gt;
&lt;br /&gt;
Essentially, the proposal is that &amp;amp;lt;script&amp;gt; tags directly support being declared with a `type` value of &amp;quot;text/cache&amp;quot;. The behavior would be that the script resource is fetched, as the load events fired, as normal, but that the script itself would not be executed. Then later, at the desired time, the script in that element could be executed by changing the `type` value from &amp;quot;text/cache&amp;quot; to &amp;quot;text/javascript&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
The browser would probably need to keep track (via some internal flag) that the script node is only ever executed once. Also, it&#039;s an open question if such a &amp;quot;cached&amp;quot; script node should be able to have its script content modified before execution via the `text` property.&lt;br /&gt;
&lt;br /&gt;
[[Category:Feature Request|Dynamic Script Execution Order]]&lt;/div&gt;</summary>
		<author><name>Getify</name></author>
	</entry>
</feed>