55

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.

Nathan Arthur
9,2977 gold badges65 silver badges83 bronze badges
asked Jan 4, 2011 at 14:08
5
  • 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? Commented Jan 4, 2011 at 14:13
  • 7
    Duplicate: stackoverflow.com/questions/326069/… Commented Jan 4, 2011 at 14:15
  • What is the differents then? :s Commented 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. Commented Jan 4, 2011 at 14:39
  • none of the answer works Commented Jan 4, 2011 at 14:40

3 Answers 3

115

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).

Nathan Arthur
9,2977 gold badges65 silver badges83 bronze badges
answered Jan 4, 2011 at 14:13
Sign up to request clarification or add additional context in comments.

6 Comments

testing top isn't so great if you expect that your frame might live in a frame off-domain. E.g. your site is hosted in someone elses via a frame / iframe reference (as sometimes happens with blog templates sites, etc). Since "top" might be in a different domain, testing parent == top can throw a cross domain error. Try putting a marker in the container frame and test for that (e.g. if (parent.myVar)
@frumbert, if 'top' might throw a cross-domain error, wouldn't 'parent' also?
@frumbert: testing a window will not throw an error(parent.myVar is not a test for a window, it's a test for a property of a window, what is slightly different)
@Dr.Molle: Yep of course. So to avoid cross domain errors you have to catch the possible exception. The answer on here : stackoverflow.com/questions/8672721/… : could be extended to test for the iframe (OP) and bail out if it hit a cross domain error, while still catching exceptions.
Nope, of course not. show 1 example where accessing a 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.
|
36

Check if window.frameElement is not null and see if its nodeName property is "IFRAME":

var isInIframe = window.frameElement && window.frameElement.nodeName == "IFRAME";
Oliver
9,5269 gold badges77 silver badges105 bronze badges
answered Jun 11, 2012 at 11:21

3 Comments

This answer is great because it contains a simple and straight-forward way to access the iframe element which contains the current page (when it's loaded inside such).
I agree with @Oliver. This way is so much simpler than the other two answers above and any other strategy I've seen for dealing with this issue. This should be the accepted answer...
This fails for cross-origin iframes. Returns the element (such as <iframe> or <object>) in which the window is embedded, or null if the element is either top-level or is embedded into a document with a different script origin; that is, in cross-origin situations. developer.mozilla.org/en-US/docs/Web/API/Window/frameElement
24
var isInIFrame = (window.location != window.parent.location);
if(isInIFrame==true){
 // iframe
}
else {
 // no iframe
}
Alex Nolasco
19.6k9 gold badges90 silver badges85 bronze badges
answered May 4, 2012 at 5:32

4 Comments

or just var isInIFrame = window.location !== window.parent.location; would work as well.
There's an edge case in which this won't work: if you've loaded the same page into the iframe that contains the iframe.
also, cross-domain or similar protection will cause a script error when accessing window.parent propery
@oriadam if it throws an error you can assume it's in an iframe

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.