Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 527
rpophessagr Avatar asked Feb 13 '12 15:02

rpophessagr


2 Answers

or just:

var isEmbed = window != window.parent; 
like image 104
Kevin Avatar answered Sep 24 '22 06:09

Kevin


Probably the simplest method:

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

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

like image 26
Šime Vidas Avatar answered Sep 21 '22 06:09

Šime Vidas