views:

206

answers:

2

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.

+2  A: 
<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.

Ape-inago
Except for forgetting the end curly, that's perfect +1
Zachary Yates
+2  A: 

window.parent: 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.)

window.top: The top level window object, even if the current window is the top level window object.

window.self: 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";
}
Benry