Closed Bug 1425101 Opened 8 years ago Closed 8 years ago

Window variable is undefined after document.open(), document.write(), document.close()

Categories

(Core :: DOM: Core & HTML, defect)

58 Branch
defect
Not set
normal

Tracking

()

RESOLVED INVALID

People

(Reporter: brianhpedersen, Unassigned)

Details

Attachments

(1 file)

Attached file index.html
User Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36 Steps to reproduce: I have a "preloader" (index.html) that populates a window.test object and then replaces the html with html fetched from an api (replace.html) with document.open(); document.write(response); document.close(). Actual results: The window.test object is undefined after the html is written Expected results: It appears in other browsers the window.test object remains intact and can reference in code in replace.html file
I was only able to add one file so the text for the replace.html file is as follows: <!DOCTYPE html> <html lang='en'> <head> <script>console.log('after', window.i3)</script> </head> <body> Hello World </body> </html>
Component: Untriaged → DOM
Product: Firefox → Core
Pretty sure the spec says document.open() should create a new window global. I'm on my mobile at the moment, so can't look up the spec/compat bugs right now. Cc'ing some folks who might have more information.
That's correct. See https://html.spec.whatwg.org/multipage/dynamic-markup-insertion.html#document-open-steps steps 16 and 17. Some other browsers are known to not follow the spec correctly here.
Status: UNCONFIRMED → RESOLVED
Closed: 8 years ago
Resolution: --- → INVALID
We should maybe open a bug to contemplate changing that though given that other browsers don't do it...
(In reply to Anne (:annevk) from comment #4) > We should maybe open a bug to contemplate changing that though given that > other browsers don't do it... That would have bigger implications. It implies the page may stay controlled by a ServiceWorker, etc. Is that really the behavior we want? It's expanding on the "single global multiple document" issues of about:blank replacement. Have the other browsers thought through all that?
I don't know, but they have that behavior, no? So it might well end up being required by web compatibility unless we either convince them to do the new global approach (not feasible I think) or convince them to make tweaks to their current model we'd be more comfortable aligning with. I'm not really sure how it relates to multiple documents, as when we don't replace the global the relationship would remain 1:1 here, I think.
I'm not inclined to switch Firefox to match behavior that isn't well thought out or defined. It's also unclear to me if document.open() behaves the same for things like service workers and clients API in chrome, edge, Safari since there is no spec for what they are doing. Do they align on corner cases?
FWIW, it seems the spec and our implementation reuses the current document object. So you can put expandos there instead of on the window global: window.foo = 'bar'; document.foo = 'bar'; document.open(); document.close(); console.log(window.foo); // null console.log(document.foo); // 'bar The document expando approach seems to work in firefox, chrome, and safari. I haven't tested edge.
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Creator:
Created:
Updated:
Size: