javascript - window.parent is not undefined? - Stack Overflow

I have a single page. with no Iframes : And I need to check if the page is in Iframe , so I did this :

I have a single page. with no Iframes :

And I need to check if the page is in Iframe , so I did this :

if (!window.parent) {...not in iframe...}

But it seems that window.parent is never undefined and always reference to the window (self===parent).

Why is that ?

the window has no(!) parent. so why it is defined ?

NB

  • I know that window.window.w.... is the same for a reason. but im talking about parent(!)

  • I know I can check this condition :

if (window.self===window.parent)...

But still my question remains.

I have a single page. with no Iframes :

And I need to check if the page is in Iframe , so I did this :

if (!window.parent) {...not in iframe...}

But it seems that window.parent is never undefined and always reference to the window (self===parent).

Why is that ?

the window has no(!) parent. so why it is defined ?

NB

  • I know that window.window.w.... is the same for a reason. but im talking about parent(!)

  • I know I can check this condition :

if (window.self===window.parent)...

But still my question remains.

Share Improve this question asked Dec 24, 2013 at 8:05 Royi NamirRoyi Namir 149k144 gold badges492 silver badges829 bronze badges 1
  • 1 Interesting find. From Microsoft ref IE, msdn.microsoft./en-us/library/ms952669.aspx. No explanation why though. – Gavin Commented Dec 24, 2013 at 8:22
Add a ment  | 

2 Answers 2

Reset to default 6

From MDN:

If a window does not have a parent, its parent property is a reference to itself

That's just the way it is.

In their example, they provide a way of achieving your requirement (slightly different angle than your solution):

if (window.parent != window.top) {
  // we're deeper than one down
}

More References:

W3

The value of the parent attribute of a Window object MUST be the parent document's Window object or the document's Window object if there is no parent document

You can pare document.referrer with window.location.href

JSFiddle

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1742295755a4417080.html

相关推荐

  • javascript - window.parent is not undefined? - Stack Overflow

    I have a single page. with no Iframes : And I need to check if the page is in Iframe , so I did this :

    17小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信