Is my page framed?

JavaScript FAQ | JavaScript Frames FAQ  

Question: Is my page framed (loaded in a frameset) or is it in the browser's top-level window?

Answer: To test whether your page is in a frameset, you can use this conditional operator:

if (self != top)  // if yes, you're framed
if (self == top)  // here you aren't framed
Example. Let's assume that your site uses frames to provide some navigation functionality (like this JavaScript FAQ site). Assume that your top-level frameset is in the file //. What if a user arrives to some of your pages without frames? (This might happen if the user arrived e.g. from a search engine.) For such users, you may want to display an additional hyperlink to your top-level frameset – only if your page is not in the frameset yet.
if (self==top) {
  // The page is not in the frameset / !!! 
 document.write('<p><a href="//">Home page</a>')
A similar code is used on this page. If you open this page in the top-level window of your browser, you'll see a hyperlink Contents (at the top of the page, right under the page title). This hyperlink will bring you back to the framed view.

See also:


Copyright © 1996-2018,