11

I'm loading a page into an iframe. Both pages are on the same domain. I want the page being loaded to do specific js functionality only if it has been loaded into an iframe. Is this possible?

Bonus: can it be done in jQuery?

Thanks

rpophessagr
  • 905
  • 3
  • 10
  • 17
  • 2
    This sounds like a job for the Circa 1997 `self==top` trick – Jamiec Feb 13 '12 at 15:50
  • 2
    possible duplicate of [How to identify if a webpage is being loaded inside an iframe or directly into the browser window?](http://stackoverflow.com/questions/326069/how-to-identify-if-a-webpage-is-being-loaded-inside-an-iframe-or-directly-into-t) – Sjoerd Feb 13 '12 at 15:51

3 Answers3

22

or just:

var isEmbed = window != window.parent; 
Kevin
  • 1,147
  • 11
  • 21
9

Probably the simplest method:

if ( self !== top ) {
    // you're in an iframe
}

So, you check if the current window is the topmost window...

Šime Vidas
  • 182,163
  • 62
  • 281
  • 385
-2

You could use iframe's onload event:

<html>
<head>
<script type="text/javascript">
function load()
{
alert("Iframe is loaded");
}
</script>
</head>

<iframe onload="load()" src="/page.html">
</iframe>    
</html>