Bug 40068 - Second document.write() of external script fails
Summary: Second document.write() of external script fails
Alias: None
Product: WebKit
Classification: Unclassified
Component: DOM (show other bugs)
Version: 528+ (Nightly build)
Hardware: All All
: P2 Minor
Assignee: Nobody
Depends on:
Reported: 2010-06-02 10:25 PDT by Tony Gentilcore
Modified: 2022-08-14 14:16 PDT (History)
6 users (show)

See Also:

Testcase (498 bytes, text/html)
2010-06-02 12:25 PDT, Tony Gentilcore
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Tony Gentilcore 2010-06-02 10:25:31 PDT
The second document.write() of an external script fails. Here's a minimal test case.

This alerts "[object HTMLScriptElement]", "null":
    document.write('<script src=ignored.js id=foo></scri'+'pt>');
    document.write('<script src=ignored.js id=bar></scri'+'pt>');

I'd expect it to work like writing two inline scripts. This alerts "[object HTMLScriptElement]", "[object HTMLScriptElement]":
    document.write('<script id=foo></scri'+'pt>');
    document.write('<script id=bar></scri'+'pt>');
Comment 1 Tony Gentilcore 2010-06-02 10:34:54 PDT
Thinking about this more it makes some sense.

The first write causes the parser to be blocked by the external script. While the parser is blocked, the script continues executing. It executes the second document.write(), but its content is parser-blocked. Thus at the point the getElementById('bar') executes, the parsed source does not contain the 'bar' script yet.

The only way to fix this would be to pause script execution until the external script executes. It isn't clear to me if that is the way it should work or if this is working as intended. Perhaps HTML5 defines this behavior? Thoughts?
Comment 2 Eric Seidel (no email) 2010-06-02 11:31:37 PDT
Is this HTML5 only?
Comment 3 Tony Gentilcore 2010-06-02 11:33:04 PDT
This is without the HTML5 parser. I haven't tested it with HTML5.(In reply to comment #2)
> Is this HTML5 only?

This is without HTML5. I haven't tried it with the HTML5 parser.
Comment 4 Eric Seidel (no email) 2010-06-02 11:34:51 PDT
We should attach your test case to this bug, and see what the results are in firefox/IE.  This behavior may or may not be expected.
Comment 5 Tony Gentilcore 2010-06-02 12:25:26 PDT
Created attachment 57680 [details]

Opera 10.5: PASS
FF3.6: RACE (passes reliably if an alert is placed after first write)
WebKit: FAIL
Comment 6 Ahmad Saleem 2022-07-29 16:59:57 PDT
All browsers (Safari 15.6, Chrome Canary 106 and Firefox Nightly 105 on macOS 12.5) match with each other in the attached test case and show "FAIL". I am not sure on web-spec whether all browsers were wrong or it is just wrong behaviour across all b browsers but just wanted to share latest test results. Thanks!
Comment 7 Alexey Proskuryakov 2022-08-14 14:16:41 PDT
The test itself is now broken, as the written scripts don't show up in the DOM until later. But they do, and can be seen in Web Inspector in all browsers.

Still, this is fixed (config changed).