Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.parent is not undefined?

Tags:

javascript

I have a single page. with no Iframes :

enter image description here

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.

like image 968
Royi Namir Avatar asked Dec 24 '13 08:12

Royi Namir


People also ask

Can Windows parent undefined?

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

What is window parent in Javascript?

The Window. parent property is a reference to the parent of the current window or subframe. If a window does not have a parent, its parent property is a reference to itself.


1 Answers

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

like image 108
CodingIntrigue Avatar answered Oct 17 '22 08:10

CodingIntrigue