How to detect if your in a frame via JavaScript
by Cory Rauch 2008-11-26 Category: JavaScript

At times you may need the ability to detect if your javascript code is in an html frame. I will cover a simple way to do this.

Below is a simple conditional statement to detect if you are in an html frame.

if(top != self) {
alert("In an HTML frame");
}

The reason this works is the current window is referenced by self, and the top most window is referenced by top. So if they do not compare we can assume we are in a HTML frame. I should note also that frames have domain restrictions in all browsers. This means that html frames loaded from a different domain then the parent page or other frames on the page, then it will not have access to those frames or pages.

Other ImprovedSource Articles:
JavaScript bracket notation allows you to use strange variable names
How to do custom sorting in Javascript on arrays
Free JavaScript Solitaire Game

Valid HTML 4.01 Transitional