[whatwg] When do scripts execute? (4.3.1)

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 [1] after poking through the archives a bit, and it's somewhat related, but doesn't address this issue directly.

<div id="r">NOTRUN</div>
<script type="text/javascript">
 var sn = document.createElement('script');
 sn.setAttribute( 'type', 'text/javascript' );
 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' );
 sn.setAttribute( 'type', 'text/javascript' );
 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?
</script>

[1] http://lists.whatwg.org/htdig.cgi/whatwg-whatwg.org/2007-May/011561.html

Cheers,
kats

Received on Thursday, 15 January 2009 07:48:30 UTC