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 /index.htm. 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 index.htm !!! 
 document.write('<p><a href="/index.htm">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:

  • Can I update two (or more) frames at a time?
  • External files in layers or IFRAMEs
  • Determining the browser window size
  • Determining the screen size
  • Copyright © 1999-2011,