Ask your JavaScript questions! Pay money and get answers fast! (more info)

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)

2012-12-12

Ross Wilson answers:


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


This will be inside an iframe
http://jsfiddle.net/FzLJ9/embedded/result/

This is just the plain page
http://fiddle.jshell.net/FzLJ9/show/light/

2012-12-13

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.


http://www.dwuser.com/education/content/responsive-design-a-crash-course-and-demo/
http://www.w3resource.com/twitter-bootstrap/responsive-design.php

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)