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
  • 8,287
  • 7
  • 55
  • 80
Mohan Ram
  • 8,345
  • 25
  • 81
  • 130

3 Answers3

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

Nathan Arthur
  • 8,287
  • 7
  • 55
  • 80
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
  • 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 '12 at 00:28
  • @frumbert, if 'top' might throw a cross-domain error, wouldn't 'parent' also? – dsdsdsdsd Feb 05 '13 at 08: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 05 '13 at 09:16
  • @Dr.Molle: Yep of course. So to avoid cross domain errors you have to catch the possible exception. The answer on here : http://stackoverflow.com/questions/8672721/get-highest-frame-that-still-is-on-domain : 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 06 '13 at 00: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 06 '13 at 00:48
  • There may also be an issue with detecting iFrame-ness before the iFrame has been added to its parent, though I'm by no means certain of this and it may be an edge case or due to the fact I'm working in Safari here. – Ash Nov 28 '13 at 08:46
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,239
  • 9
  • 69
  • 100
Pink Duck
  • 561
  • 5
  • 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 '14 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 '15 at 21:19
  • 2
    This fails for cross-origin iframes. Returns the element (such as – Ryan Mohr Aug 03 '18 at 19:11
24
var isInIFrame = (window.location != window.parent.location);
if(isInIFrame==true){
    // iframe
}
else {
    // no iframe
}
Alex Nolasco
  • 18,750
  • 9
  • 86
  • 81
Sumith Harshan
  • 6,325
  • 2
  • 36
  • 35