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>
Potentially similar posts
- How to: Get a white screen – June 2011
- Viewing dynamically generated HTML in the HTML Help viewer – November 2010
- Watch out for PHP shorthand – November 2010
- Online JavaScript scratchpads – November 2010
- Use the existence of a file on the server to determine Javascript behaviour in the browser – November 2010