Check if the parent window is an iframe or not.

How can I point to the page inside the iframe if the parent itself is also inside the iframe?

Explanation:

My home.html contains iframe

 <iframe src="sample.html"></iframe> 

I need to determine if home.html (i.e. the parent element of sample.html ) is inside 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 duplicated. This is a different case.

+47
javascript jquery iframe
Jan 04 2018-11-14T00:
source share
3 answers

This is true if the window is not an iframe:

 if(self==top) 

If you like to see if the parent window of this window is a frame, use:

 if(parent==top) 

This is a simple comparison of top (the topmost window of a window hierarchy) and another window object ( self or parent ).

+95
Jan 04 2018-11-11T00:
source share

Make sure window.frameElement not null and sees if its nodeName property is "IFRAME":

 var isInIframe = window.frameElement && window.frameElement.nodeName == "IFRAME"; 
+29
Jun 11 '12 at 11:21
source share
 var isInIFrame = (window.location != window.parent.location); if(isInIFrame==true){ // iframe } else { // no iframe } 
+20
May 04 '12 at 5:32
source share



All Articles