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.
-
It's a bit unclear... Do you have an iframe in a page in an iframe? Do you want to check if a page is in an iframe, or if the page containing the iframe is in an iframe?Guffa– Guffa2011年01月04日 14:13:13 +00:00Commented Jan 4, 2011 at 14:13
-
7Duplicate: stackoverflow.com/questions/326069/…Kimtho6– Kimtho62011年01月04日 14:15:47 +00:00Commented Jan 4, 2011 at 14:15
-
What is the differents then? :sKimtho6– Kimtho62011年01月04日 14:36:48 +00:00Commented Jan 4, 2011 at 14:36
-
once home.html is an iframe then i need to detect home.html is an iframe from sample.html.Mohan Ram– Mohan Ram2011年01月04日 14:39:46 +00:00Commented Jan 4, 2011 at 14:39
-
none of the answer worksMohan Ram– Mohan Ram2011年01月04日 14:40:09 +00:00Commented Jan 4, 2011 at 14:40
3 Answers 3
This is true if a window is not a frame/iframe:
if(self==top)
If you like to see if the parent window of the given window is a frame, use:
if(parent==top)
It's a simple comparision of top (the most top window of the window hierarchy) and another window object (self or parent).
6 Comments
window-object results in an cross-domain-error(that's all we talk about here, window-objects, nothing more). The linked question has nothing to do with the question here.Check if window.frameElement is not null and see if its nodeName property is "IFRAME":
var isInIframe = window.frameElement && window.frameElement.nodeName == "IFRAME";
3 Comments
iframe element which contains the current page (when it's loaded inside such).var isInIFrame = (window.location != window.parent.location);
if(isInIFrame==true){
// iframe
}
else {
// no iframe
}
4 Comments
var isInIFrame = window.location !== window.parent.location; would work as well.