5

Consider the following top-level javascript code:

if (this.window === window)
    alert('same');
else
    alert('different'); // alerts: different  

Why is this.window and window not strictly equal? I've also tried 'this' on the rhs of the expression and get the same result.

5
  • this.window === window returns true in Chrome.
    – Jeremy
    Nov 14, 2010 at 22:19
  • What browser are you using? Both Firefox and Chrome return true for this.window === window. Or do you have any situation with Iframes? Nov 14, 2010 at 22:22
  • 1
    If you're really getting that this !== window, I believe you must be running the code not in the top level. If this === window it follows that this.window === window as window.window === window. Nov 14, 2010 at 23:01
  • 1
    Aidas and Dylan were corrrect, this is an IE8 peculiarity it seeems. (this===window returns true, this.window===window returs false)
    – Seth Stone
    Nov 15, 2010 at 0:09
  • what does ( window.window === window.window.window ) return in IE8?
    – code_monk
    Nov 22, 2014 at 21:05

4 Answers 4

8

In Internet Explorer (8.0.7600 is what I've tested), this with no qualifier actually resolves to the global window object. In all other browsers I've tried (Chrome, Firefox, Opera), this.window === window in that context - and, helpfully, this === window as well.

Try this in IE to verify:

if (this === window)
  alert('same');
else
  alert('different');
2
  • 1
    In the example you gave, 'same' is alerted in IE8. I think you most precisely answered my original question... that this is really a pecularity to IE8.
    – Seth Stone
    Nov 15, 2010 at 0:05
  • 1
    The way IE8 treats this kind of makes more sense to me. this.window seems equivalent to window.window. Apr 9, 2012 at 3:26
3

It seems as though HTML elements do not contain a pointer back to their parent window, as it does for parentNode. Thus, this.window will return undefined when this is anything other than a window object.

The window object seems to be able to reference itself, perhaps because it is the only node high enough to "see" itself. Thus, window == window.window.window.window and so on.

The idiosyncrasies between browsers seem to do with how each implements the DOM structure, and in particular, how they interpret this at the top-level.

Seeing as how individual HTML elements can't reference their parent window with .window, I don't really see a point in ever using this.window, though I'd love to be proved wrong here.

If you're working on code that involves manipulating objects across two different windows, I would suggest assigning your new window to a variable, e.g. var newWin = window.open(...) and subsequently using this variable to reference new objects.

5
  • Extra bit on native JavaScript code: provided this === window in the current scope, function t(){return this};; t() === window, (x = new t()) === x (not window). Nov 14, 2010 at 23:33
  • I believe the handling of this is defined rigidly for the top level; I know that at least Processing.js uses this instead of window, apparently because it's shorter (I asked because it surprised me when I saw it). In the global scope, this === window should always be true. Nov 14, 2010 at 23:36
  • 1
    @Chris: If you're trying to access the global object then you should use this rather than window if you want your code to run in non-browser environments. It's not true that window and the global object are precisely the same thing, although for most purposes in browsers they can be thought of as being so.
    – Tim Down
    Nov 14, 2010 at 23:53
  • @Tim: I suppose that makes sense; however, Processing.js is targeting only browsers (due to its nature, using <canvas>, etc.) Nov 15, 2010 at 0:04
  • @Jeff - I would upvote this, but I don't have enough cred yet. :-(
    – Seth Stone
    Nov 15, 2010 at 0:06
2

Works here...

http://jsfiddle.net/rygar/DQYdk/

1
  • He said it was "top level javascript code", so your first answer was correct and your statement that you were wrong is wrong. It does work and is true. Nov 14, 2010 at 22:56
0

Because this equals window in global context.

1
  • ... and so this.window === window as window is a global variable - window.window === window. window.window.window === window ;-) Nov 14, 2010 at 22:57

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.