Test whether a page is in a frame

November 28th, 2006

You can use the following JavaScript to test whether the current page is being displayed as a frame within a frameset, or is being displayed as a standard, standalone Web page.

In this example I'm just displaying a message, in real life you could use this to change what's displayed on the page. For example, I'm using similar code in a linked JavaScript file to show or hide a div on the page. I use this in a Flare Help project so that, when a topic is viewed as a standalone page a link is displayed allowing the user to view the page within the 3-frame Web Help (i.e. topic frame, navigation frame and toolbar frame).

<script language="javascript">
<!--
function showHideHelpLink() {
   if (location.href == parent.location.href)  {
     alert("single page");
   }
   else {
     alert("I'm in a frame");
   }
}
// -->
</script>

Leave a comment