[whatwg] defer on style, depends

Boris Zbarsky bzbarsky at MIT.EDU
Sun Feb 15 13:08:07 PST 2009


Ian Hickson wrote:
> On Fri, 13 Feb 2009, Boris Zbarsky wrote:
>> The current Gecko behavior is that any stylesheet load started by parsing a
>> <style> or <link> tag will increment a counter on the document (well, on a
>> per-document script loader object, to be more precise). Completion of the load
>> will decrement the counter.  While the counter is nonzero, <script> execution
>> is blocked.  When it goes back to 0, the first pending script (if any) is run.
>> If this increments the counter again, no more scripts are run until the count
>> goes to 0 again.
>>
>> So it doesn't matter how the script is created/inserted, but the only
>> stylesheets that block scripts are ones that the parser knows about.  So only
>> the ones present in the original source or added via document.write.  If you
>> createElement a <link> and insert it into the DOM, it won't block script
>> execution.  Also, <link> elements pointing to alternate style sheets don't
>> block script execution.
> 
> So in this:
> 
>    <!DOCTYPE html>
>    ...
>    <script>
>     document.write('<link rel=stylesheet href=style>');
>     document.write('<script>a();<\/script>');
>     b();
>    </script>
> 
> ...is the script paused after the second document.write() call, before a() 
> and b() execute?

No.  What's paused is execution of new scripts, not of existing ones. 
So in this case, b() executes immediately, while a() executes after the 
stylesheet loads.

Here's a simple testcase to demonstrate that:

    <!DOCTYPE html>
    <script>
     var res = "";
     document.write('<link rel=stylesheet href="data:text/css,">');
     document.write('<script>res+="a"<\/script>');
     res+="b";
    </script>
    <body onload="alert(res)"></body>

I realize that this does mean that the execution order of a() and b() is 
different if the <link> is written out as above....

-Boris



More information about the whatwg mailing list