简体   繁体   中英

Preventing a page to be shown outside a iframe

I have a page which work like a navigation and a iframe in this page which show the content.

Now there are some situation when the inner page is directly shown in the browser. eg: if somebody types the inner page's url in the browser address bar, the page is displayed in the window.

I want to prevent this.

Better still, I would like to redirect to any other page.

: The window object that contains the frame. :包含框架的窗口对象。 If the the window is the top level window then window.parent refers the window itself. (It is never null.)

: The top level window object, even if the current window is the top level window object. :顶级窗口对象,即使当前窗口是顶级窗口对象。

: The current window object. :当前窗口对象。 (It is a synonym of window.)

So, I'd write my check like this:

if (window.top == window.self) {
  window.location = "index.html";
}

Which would be identical to the slightly more ambiguous:

if (window.top == window) {
  window.location = "index.html";
}
<script language="Javascript"><!-- 
if (top.location == self.location) { 
  top.location = "index.html" // must be viewed in main index
}
//--></script>

modified it from a situation where an an iframe decides to become the main frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM