[whatwg] What should document.write() do when called from setTimeout or event handlers?

Andrew Fedoniouk news at terrainformatica.com
Mon Jul 31 19:32:31 PDT 2006

----- Original Message ----- 
From: "Ian Hickson" <ian at hixie.ch>
To: <whatwg at whatwg.org>
Sent: Monday, July 31, 2006 3:34 PM
Subject: [whatwg] What should document.write() do when called from 
setTimeout or event handlers?

> I'm trying to spec document.write() and I've run into a difficult edge
> case.
> Imagine the server sees this:
>   <html>
>    <head>
>     function test() {
>       document.write('TEST');
>     }
>     setTimeout(test, 1000);
>    </head>
>    <body>
>     <p><img src="test.png" alt="" onload="test()"></p>
> ...and then time passes. The image loads, the timeout fires.
> Then once the image has loaded and the timer has fired:
>    </body>
>   </html>
> ...and the connection is closed.
> What should happen?
> IE seems to make those calls to document.write() simply blow away the
> document, as if the document was closed. Opera seems to do the same.
> Mozilla seems to make the document.write() calls insert text into the
> parser, as if they'd been called inline, with the result that the inserted
> text could appear pretty much anywhere. (It's actually a bit more complex
> than that -- it avoids inserting into tokens -- but that's a detail.)
> I couldn't work out what Safari and MacIE do; they seem to delay the
> timeout somehow and then print to the end of the page.
> Any preferences? I'm particularly interested in feedback from browser
> developers here as to whether there is anything I should know about how
> easy/hard it is to do one thing or the other.

(That is what I never understand : why script is allowed to do anything
during load time. Script should start executing when DOM is complete,
when, e.g. getElementById makes real sense.)

>From implementation point of view: all events shall be disabled
until "original DOM complete" state (</html> parsed and processed).
Precisely - events shall be postponed (probably some of them may just
be discarded). First event that shall be fired is window.onload (?)

This is easy to implement and deterministic - read can be implemented
uniformely in all UAs.

Normaly when document.write appears in the <script> section
body of the script has been loaded in full so insertion point for the
write is known - end of the script block.
document.write in other circumstances (event handlers) shall use
end of the body element (?) as an append point.

Andrew Fedoniouk.

More information about the whatwg mailing list