0

My problem is that I have two html files for example say 1.html and 2.html. The contents of the files are 1.html It consists of the Iframe. The source of the Iframe is 2.html. 2.html It is a sample html page.

My question is that I want to check whether the 2.html is loaded on an Iframe or loaded on a separate browser directly without putting it inside an Iframe. The checking has to be done from 2.html only.

Any suggestions friends. Thanks in advance.

AmGates
  • 2,127
  • 16
  • 29
  • 1
    Possible duplicate of http://stackoverflow.com/questions/4594492/to-check-parent-window-is-iframe-or-not – Paul Bruno Mar 19 '12 at 07:40
  • You can use onload event on the iframe. but the problem is that the onload event doesn't fire on ie browser. To possible check the loading of the file u can use ajax request – Kunal Vashist Mar 19 '12 at 07:45

4 Answers4

5

when loaded in iframe the window.parent points to the parent window, however when loaded in a separate window window.parent points to window itself:

var loadinInIframe = window.parent != window;
Sepehr
  • 158
  • 6
0

with javascript you can check using document.location if window.parent.location and window.location, if parent location is not your app location then you might be iFramed

Ibrahim
  • 342
  • 4
  • 14
0

Bind a function inside the iframe's onload event and set a loaded variable on the parent page

On the iframe

window.onload = function() {
   parent.iframeLoaded = true;
}

On the parent page, just declare the variable to hold the value

var iframeLoaded = false;

Now you can check this var when you need from the parent page like

if(iframeLoaded) {
    // Do something
}
Starx
  • 77,474
  • 47
  • 185
  • 261
0
if (top === self) { not in a frame } else { in a frame }

Top and self are both window objects (along with parent), so you're seeing if your window is the top window.

Credit to this. Hope that helps.

Community
  • 1
  • 1
shams.haq
  • 703
  • 1
  • 6
  • 20