rpophessagr
rpophessagr

Reputation: 905

As a page, can I tell if I've been loaded into an iframe using javascript?

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

Upvotes: 11

Views: 8081

Answers (3)

Ivan Kolodyazhny
Ivan Kolodyazhny

Reputation: 574

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>

Upvotes: -2

Šime Vidas
Šime Vidas

Reputation: 186103

Probably the simplest method:

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

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

Upvotes: 9

Kevin
Kevin

Reputation: 1157

or just:

var isEmbed = window != window.parent; 

Upvotes: 22

Related Questions