[whatwg] When do scripts execute? (4.3.1)
ian at hixie.ch
Wed Feb 11 17:23:13 PST 2009
On Thu, 15 Jan 2009, Kartikaya Gupta wrote:
> I have a question about when scripts execute. From my reading of section
> 4.3.1 it seems like if script elements are created and added to the DOM
> via DOM operations, then they should NOT be marked as parser-inserted,
> and should get run immediately (assuming no defer/async stuff). However,
> a page that I constructed to test this (below) fails in FF, Opera, and
> Safari (didn't test IE). Am I misreading the spec, or does it not
> accurately reflect current behavior? I found  after poking through
> the archives a bit, and it's somewhat related, but doesn't address this
> issue directly.
> <div id="r">NOTRUN</div>
> var sn = document.createElement('script');
> sn.appendChild( document.createTextNode( 'document.getElementById("r").firstChild.data = "PASS";' ) );
> document.body.appendChild( sn ); // this runs the script and sets r's text to PASS (verifiable by alert)
> sn = document.createElement( 'script' );
> document.body.appendChild( sn ); // this *should* run an empty script block and do nothing
> // the next line should have no effect since the script already ran
> sn.appendChild( document.createTextNode( 'document.getElementById("r").firstChild.data = "FAIL";' ) );
> // here r's text is FAIL. why?
>  http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2007-May/011561.html
On Thu, 15 Jan 2009, Boris Zbarsky wrote:
> That looks like a bug in the spec to me. Empty scripts with no @src are
> not executed when inserted into the DOM, and are executed if they ever
> become nonempty or have their src set.
On Thu, 15 Jan 2009, Jonas Sicking wrote:
> At least in the mozilla implementation, though would be interested what
> other implementations do. The reason was that I wanted to support the
> following usage pattern:
> s = document.createElement('script');
> s.src = "http://foo/bar";
> Note that in IE, you can set the src attribute any number of times and
> every time it will download and execute the new script, even when the
> old src value is the same as the new.
I've updated the spec to work like Boris describes. The browsers aren't
all exactly the same, but this seems like the most consistent set of
requirements that matches the most browsers.
Ian Hickson U+1047E )\._.,--....,'``. fL
http://ln.hixie.ch/ U+263A /, _.. \ _\ ;`._ ,.
Things that are impossible just take longer. `._.-(,_..'--(,_..'`-.;.'
More information about the whatwg