最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript this.window not equal to window - Stack Overflow

programmeradmin4浏览0评论

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.

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.

Share Improve this question asked Nov 14, 2010 at 22:15 Seth StoneSeth Stone 2,7621 gold badge15 silver badges13 bronze badges 5
  • this.window === window returns true in Chrome. – Jeremy Commented 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? – Aidas Bendoraitis Commented 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. – Chris Morgan Commented 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 Commented Nov 15, 2010 at 0:09
  • what does ( window.window === window.window.window ) return in IE8? – code_monk Commented Nov 22, 2014 at 21:05
Add a ment  | 

4 Answers 4

Reset to default 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');

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.

Works here...

http://jsfiddle/rygar/DQYdk/

Because this equals window in global context.

发布评论

评论列表(0)

  1. 暂无评论