分享

JavaScript and Frames, Part I: Escaping Frames

 zhuzhu 2006-02-25
Travel Ideas:
 Ft. Walton Hotels
 Hawaii Hotels
 Discount Travel
 Fort Lauderdale
 Las Vegas Hotels
Miami Hotels
 San Francisco Hotels
 Spring Break
Developer News
ADV: Sun‘s new breakthrough server
Yahoo Lays Out Welcome Mat For PHP Devs
MySQL to Fight Oracle With Fire(bird)?

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;

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多