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.

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?
    – Guffa
    Jan 4, 2011 at 14:13
  • 7
    Duplicate: stackoverflow.com/questions/326069/…
    – Kimtho6
    Jan 4, 2011 at 14:15
  • What is the differents then? :s
    – Kimtho6
    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
    Jan 4, 2011 at 14:39
  • none of the answer works
    – Mohan Ram
    Jan 4, 2011 at 14:40

3 Answers 3

116

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
  • 2
    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
    Dec 10, 2012 at 0:28
  • @frumbert, if 'top' might throw a cross-domain error, wouldn't 'parent' also?
    – dsdsdsdsd
    Feb 5, 2013 at 8:26
  • @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
    Feb 5, 2013 at 9:16
  • @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.
    – frumbert
    Feb 6, 2013 at 0:38
  • 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.
    – Dr.Molle
    Feb 6, 2013 at 0:48
36

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

var isInIframe = window.frameElement && window.frameElement.nodeName == "IFRAME";
3
  • 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).
    – Oliver
    Apr 23, 2014 at 22:01
  • 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...
    – Sheldon R.
    Apr 24, 2015 at 21:19
  • 2
    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
    – Ryan Mohr
    Aug 3, 2018 at 19:11
24
var isInIFrame = (window.location != window.parent.location);
if(isInIFrame==true){
    // iframe
}
else {
    // no iframe
}
4
  • 2
    or just var isInIFrame = window.location !== window.parent.location; would work as well.
    – Chris Barr
    Jul 12, 2012 at 18:48
  • 5
    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.
    – Oliver
    Apr 23, 2014 at 21:58
  • 3
    also, cross-domain or similar protection will cause a script error when accessing window.parent propery
    – oriadam
    Sep 24, 2015 at 11:21
  • @oriadam if it throws an error you can assume it's in an iframe
    – Stefan P
    Jun 7, 2018 at 3:18

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.