Detecting an iframe with JavaScript
Published by Nicholas Dunbar on December 24th, 2012
How do you tell if your content has loaded into an IFRAME container? I use a very simple JavaScript to detect an IFRAME.
This can be used to determine if some one is loading your content into their external page without cross domain permisions. Sometimes people hijack your content by loading it into an IFRAME and masking your website and ads as a way of making it look like your content is theirs. With this JavaScript you can make sure that does not happen. However in most cases you should not have a problem with other websites loading your content into an IFRAME as long as they are showing your ads and links you use to monitize your site. If they are not trying to mask out your monitization strategies then you should just look at this as a free helping hand to get more traffic to your site.
Detecting if you are running your Facebook Connect applicaiton on Facebook or on your site outside of the IFRAME.
It is also helpfull if you are building an IFRAME Facebook application with Facebook Connect. Sometimes you want to be able to host your app from your home page as well as on Facebook apps. In this case you don't always want it to appear the same in both cases so you can use this JavaScript to detect an IFRAME and if it is present or not. Then your JavaScript can decide on the fly how to customize the look and feel of the site. You could even remove all the Facebook Connect libraries on the fly so that the app when hosted from your site would not require Facebook.
Just some fun ideas on how to use this simple but effective way for detecting an IFRAME with JavaScript.