spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / experts / javascript / column36


JavaScript and Frames, Part I (2)

Developer News
Eclipse Helios Update Brings New PHP Tools
Internet Explorer 9 Ups Standards Support
JBoss Portal 5 Release Easier to Use

Escaping Frames

When you create a Web page, you expect the user to load it in the browser's main window. You don't want it to be loaded in another site's frame. By adding a simple cross-browser, cross-platform script into the HEAD portion of your document, you can ensure that the page loads in the entire window. Here's the script:

<SCRIPT LANGUAGE="JavaScript">
<!--

if (window != top) top.location.href = location.href;

// -->
</SCRIPT>

The condition evaluates to true only if window is not equal to top. In other words, if the page's direct window isn't the topmost window in the browser, the condition evaluates to true.

top is the highest window object in the hierarchy. If the topmost page doesn't feature any frames, the object model has only one level of window objects, so top is equal to window. However, if the browser is currently displaying a frame-based document, each child frame has a corresponding window object.

Note that the following expressions would also function properly:

window != parent
self != top

Back to the script. If the page's window isn't the topmost window in the object model, your page is being displayed as a frame in a frame-setting document. In order to escape the frames, you need to set the URL of the upper window to the URL of the current page:

if (window != top) top.location.href = location.href;

http://www.internet.com

Produced by Yehuda Shiran and Tomer Shiran

webref The latest from WebReference.com Browse >
Flashmaps' DynamicLocator: Interactive Maps for Small Areas · Flashmaps' AreaSelector: Interactive Maps for Wide Areas · The DB Mapper: Interactive Street-level Maps of U.S. and Canada
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
MS Access and MySQL · Cisco AutoQoS: VoIP QoS for Mere Mortals · While VoIP Adoption Explodes in Enterprise, Carrier Spending Lags


Created: March 1, 1999
Revised: March 1, 1999

URL: http://www.webreference.com/js/column36/escaping.html