Ask your JavaScript questions! Pay money and get answers fast! Comodo Trusted Site Seal
Official PayPal Seal

How to test if current page has a parent, or is an iframe? JavaScript

I have a page that sometimes will be shown as a normal webpage, and other times will be shown inside of jQuery Fancybox, which means it will be in an iframe. This page needs to render different HTML, depending on whether it is a plain page, or inside the iframe. How do I do this? Should I test for a parent window? If yes, what is the code for that?

Using jQuery 1.4

Answers (2)


Ross Wilson answers:

if (top.location!= self.location) {
alert("I am inside an iframe");

This will be inside an iframe

This is just the plain page


Jarret Minkler answers:

Hi Lawrence!

Have you looked into responsive design? This will also fix tablet/mobile views without having to do UA lookups etc.

When the page is different sizes, the CSS will change and you could do things like hide certain elements or adjust the layout (depending on your setup)