How can I tell from a page within an iframe, if the parent itself is also within an iframe?
Explanation:
My home page home.html
contains an iframe
<iframe src="sample.html"></iframe>
I need to detect if home.html
(ie: parent of sample.html
) is within an iframe.
Code in sample.html
:
if(self==window)
{
alert('home.html is not in iframe');
}
else
{
alert('home.html is in iframe');
}
My question is not a duplicate. It's a different case.
This is true if a window is not a frame/iframe:
If you like to see if the parent window of the given window is a frame, use:
It's a simple comparision of
top
(the most top window of the window hierarchy) and another window object (self
orparent
).Check if
window.frameElement
is not null and see if its nodeName property is "IFRAME":