I can get window.document
but how can I get document.window
? I need to know how to do this in all browsers.
-
2dare i ask why? window is an always available object and there is always no more than 1 document in a window, a 1-1 relation so to speak... – Colin Aug 27 '09 at 00:10
-
5Because I'm using prototype across multiple iframes, and the various Element methods break in IE or Safari if the element somehow gets extended in the wrong scope. I have revised some things to fix this issue in prototype, but part of the fix requires me to find which Window an element is in. – Joren Aug 27 '09 at 18:19
-
1@Joren, please consider to change to accepted answer. – Kijewski Feb 09 '16 at 15:41
6 Answers
You can go with document.defaultView
if you’re sure its a window and its okay to skip Microsoft browsers before IE 9.

- 18,334
- 18
- 100
- 135

- 102,654
- 32
- 106
- 127
-
7Yes. Just remember that as per specs, `document.defaultView` does not have to be the same object as `window` (and, it is in fact different in some older clients, e.g.: Safari 2.x) – kangax Aug 27 '09 at 02:40
-
-
4doc.parentWindow || doc.defaultView, and it works in old IE and Safari – Stefan Steiger May 31 '17 at 12:12
-
1somehow I think this should be the accepted answer? I see an awful lot of javascript S.O. questions following this format... which leads me to believe that probably defaultView either didn't exist or wasn't widely implemented when the question was originally posted... just wish that there would be way to tag answers with that info... I mean I see this all the time. Probably Stack Overflow's practices are to blame, SO seems to think any question is a duplicate question if it asks the same/similar question. I disagree... same/similar phrasing sure, but the accepted answer would often be "dated" – ycomp Jun 27 '17 at 09:09
-
1[Link to MDN reference on defaultView](https://developer.mozilla.org/en-US/docs/Web/API/Document/defaultView) – Robert Dundon Oct 11 '22 at 18:31
A cross browser solution is complicated, here's how dojo does it (from window.js::get()):
// In some IE versions (at least 6.0), document.parentWindow does not return a
// reference to the real window object (maybe a copy), so we must fix it as well
// We use IE specific execScript to attach the real window reference to
// document._parentWindow for later use
if(has("ie") && window !== document.parentWindow){
/*
In IE 6, only the variable "window" can be used to connect events (others
may be only copies).
*/
doc.parentWindow.execScript("document._parentWindow = window;", "Javascript");
//to prevent memory leak, unset it after use
//another possibility is to add an onUnload handler which seems overkill to me (liucougar)
var win = doc._parentWindow;
doc._parentWindow = null;
return win; // Window
}
return doc.parentWindow || doc.defaultView; // Window
has("ie") returns true for IE (and false otherwise)

- 1,792
- 14
- 29
Well, this is the solution I went with. It works, but I hate it.
getScope : function(element) {
var iframes = top.$$('iframe');
var iframe = iframes.find(function(element, i) {
return top[i.id] ? top[i.id].document == element.ownerDocument : false;
}.bind(this, element));
return iframe ? top[iframe.id] : top;
}

- 9,623
- 19
- 63
- 104
-
What's IE only about it? Seems to work fine in FF\Saf\IE\Chrome. It's not necessary in anything but IE however, since the other browsers all extend the elements automatically in the right scope. – Joren Aug 31 '09 at 23:15
I opted to inject the DOCUMENT
token from @angular/platform-browser
:
import { DOCUMENT } from '@angular/platform-browser'
and then access the parent:
constructor(@Inject(DOCUMENT) private document: any) {
}
public ngOnInit() {
// this.document.defaultView || this.document.parentWindow;
}

- 10,313
- 15
- 75
- 118
first off let's be clear. this sort of thing is often necessary when you are working with frames, iframes, and multiple windows, and so "the window is just the global object" is an unsatisfying answer if all you have a handle to is a document from another window than the one you are in.
second, unfortunately there is no direct way of getting at the window object. there are indirect ways.
the primary mechanism to use is window.name. when creating a window or a frame from some parent window, you can usually give it a unique name. any scripts inside that window can get at window.name. any scripts outside the window can get at the window.name of all its child windows.
to get more specific than that requires more info about the specific situation. however in any situation where the child scripts can communicate with parent scripts or vice versa, they can always identify each other by name, and this is usually enough.

- 15,401
- 3
- 59
- 76
The Window object is the top level object in the JavaScript hierarchy, so just refer to it as window
Edit: Original answer before Promote JS effort. JavaScript technologies overview on Mozilla Developer Network says:
In a browser environment, this global object is the window object.
Edit 2: After reading the author's comment to his question (and getting downvotes), this seems to be related to the iframe's document window. Take a look at window.parent and window.top and maybe compare them to infer your document window.
if (window.parent != window.top) {
// we're deeper than one down
}

- 41,386
- 23
- 126
- 155
-
8After `open()` the new window's `window` is another `window` than the `opener`. If you receive a `document` (or any `Node`) from some function, you might need the original window. (Wow this is very old.) – Rudie Mar 21 '13 at 18:08