[whatwg] scripts, defer, document.write and DOMContentLoaded

On Tue, 7 Jul 2009, Jonas Sicking wrote:
> 
> What's tricky is that a document.write inside a deferred script in IE
> will in some circumstances clear the current document, in other cases
> append to it. Specifically it seems that if the page has ever set
> .innerHTML on any node then document.write in the deferred script will
> append to the current page. If .innerHTML has never been set, then it
> will replace the current page.

Actually what's going on is more subtle than that. When you set innerHTML, 
it's actually triggering the deferred scripts right there, if it has them 
loaded (e.g. inline scripts or cached scripts). If it doesn't have them 
loaded yet, it drops them on the floor and doesn't ever run them.

I've specced this, except that the spec requires that not-yet-loaded 
scripts be loaded then run, rather than dropped, before innerHTML 
continues, so there's no race conditions.


> [...] So all in all, I'd like to see the following changes to the spec:
> 
> * While executing deferred scripts, let the "insertion point" be at
> the end of the document.

I've specced the actual IE behaviour with innerHTML instead.


> * Don't fire DOMContentLoaded until all deferred scripts have executed.
> * Possibly hold off firing DOMContentLoaded until any outstanding
> scripts have finished loading and executing.

I've done this. In fact, I've made it wait until all scripts that were 
pending when the parsing finished have been run.

(You mentioned .readyState; I haven't changed that, since it makes the 
'interactive' state far less useful if it only gets set once all the 
scripts have run but before the images have loaded. Being able to detect 
when or whether parsing has finished while running deferred scripts does 
seem useful. Hopefully it's not too much rope.)


> * Always execute elements in the order they are inserted into the
> Document, with exception of async and deferred scripts.

I haven't done this, because people use document.appendChild() of external 
scripts specifically to have scripts run "ASAP" and not necessarily in 
order.

In general, I am very wary of changing this part of the spec, as it was 
written with extreme care based on all the tests I could run, primarily on 
IE, but also on other browsers. The innerHTML thing, though... I'll admit 
I really didn't even remotely think that there could be something to test!

-- 
Ian Hickson               U+1047E                )\._.,--....,'``.    fL
http://ln.hixie.ch/       U+263A                /,   _.. \   _\  ;`._ ,.
Things that are impossible just take longer.   `._.-(,_..'--(,_..'`-.;.'

Received on Monday, 20 July 2009 19:25:05 UTC