We help IT Professionals succeed at work.

Force Page to load in _top

willman asked
Medium Priority
Last Modified: 2012-05-04
For various reasons, and various users, a certain page of mine is sometimes accidentally getting loaded within a frame.  This particular page looks REALLY bad within a frame.  I was wondering if there was a tag or Jscript (both IE and Netscape compatible) that would force my page to load as a parent document even if someone is loading it from within their frame.
Watch Question

If you load your page in a framed environment, then you could
put a refresh statement in your pages to call your main page to _TOP.  Even call another frameset.

The refresh scheme will not work unless the framed page is loaded, someone loads/inks to it. If you put the meta refresh statement in your main page it might continually reload unless you put a loop="0" clause, or loop="no" either clause may not work. (I would not try the refresh clause on my main page.)

This META tag is to be used in a framed environment. The content="1 part of the statement is to set the
time factor for the refresh statement. The URL=page2.html" is your target or main page.

Frame loads two pages. when section two loads, it calls a refresh statement that calls your main document to the _top.


Your frameset can be something like this:

<head><title>frame to reload page</title></head>
<frameset rows="98%,*">
<frame src="main.html" name="main" scrolling=auto noresize >
<frame src="reload.html" name="active" order="no">

You put the refresh meta statement in reload.html

refresh.html would read something like this:

<META HTTP-EQUIV=REFRESH CONTENT="1; URL=main.html" target="_top">

The following line will give the person reading your page the manual option of breaking out of the frame if the refresh scheme does not work.

<A HREF="main.html" target="_top">Home</A>

You could place this "_target" statement on all your links to always break out of frames whenever someone applies one of your links  too. It is an easy way but does not automatically happen when your initial page is loaded.

<A HREF="document2.html" target="_top">Break out of frame</A>

I have not tried the meta statement because I really do not mind someone linking to my pages. Does your page have your identity
stamps on them? copyright, mailto, other important page links?

This question was asked earlier.


This just makes the page continuously reload.  It does break out of the frame, but the page continues to reload.  This is not an acceptable solution.

Pass the following fucntion to the browser onLoad - it will check to see if the parent location is the same URL as the document itself and, if not, will load the document into the parent frameset (if a frameset is nested inside a frameset, it will function recursively)

if (parent.document.location != 'page_URL'){
      parent.document.location = 'page_URL';


Nope -- In Internet Explorer, this produces an error that this object does not have the specified property (or something like that).  In Netscape, this still causes the page to continuously reload.
Hi willman,

The page will continually reload if the refresh statement is on the page being reloaded. Reason for he framed environment.

Which page is being sent to _top?  Should be your main.html. the frameset is now gone as well as refresh.html.   The frame loads, refresh.html calls your main page to _top.  As your page is now on top, with no refresh statement, how could it reload?

Comment your code back, so we can review it. there should be at least three pages:
1.  frameset.html
2.  main.html <!empty doc is fine.>
3.  refresh.htm.


I have fiddled a little, and this is the result:

index.html (my default document):
<META HTTP-EQUIV=REFRESH CONTENT="1; URL=main.html" target="_top">

bunch of stuff

The result:  It now works fine for those linking directly to the page(it no longer continuously reloads).  However, for those who are linking from a frame, it does not break out of the frame (it seems to be ignoring the TARGET command.
From what I see, you are loading your index file which then calls your main doc to the top.  This will work so long as they call your index or leader file.whatever

you may want to give your leading.doc some other name which can include uppercase letters to make it just a bit more difficult to load the page indirectly.

Anyway, I just got a newsletter that should solve your problem. It is part of a document Chuch Musiano wrote for Netscape World which says that this problem can only be solved using java.  hmmm.

The Java script first checks to see if it is loaded in a framed environment.  If so, it call the second part of the scheme to break out to top.

Try the link to the page and see if it makes sense.  



Collecting all my favorite pieces of the puzzle, I offer this version of the <body> tag to frame-proof your pages:

     <body onLoad="if (self != top) top.location = self.location">


Seems simple enough, his claims are on the link.  

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Hey willman,

Here is the text of the netscapeworld document, as referenced above.


Stop the barrage of e-mail! I give: you can frame-proof your pages, keeping them from being captured by the frames on another page that links to your site.
     In my defense, I did claim that frame-proofing is impossible using HTML. All the solutions I received used JavaScript to escape from frames. Since
     JavaScript is technically not part of HTML, I feel somewhat vindicated that my answer was correct, although of little use to the majority of my readership.

     To recap, a reader wrote complaining that their beautiful pages were being referenced from within frames on other sites. On those sites, the link to their site
     did not contain a target=_top attribute, causing their pages to be squeezed into a frame within the linking page. The result was ugly and bothersome,
     especially since the reader's pages were being diminished by someone else's HTML shortcomings. How could he keep this from happening?

     The answer lies not in HTML, but in JavaScript. It only takes a few lines of JavaScript to detect the presence of frames and to then remove them.

     I was struck by how many different ways authors had come up with to detect that frames were being used on the current page. All of the following JavaScript
     conditional expressions will test to see if frames are in use:
          if (self.location != top.location)
          This tests to see if the document location of the current document matches the location of the top-most document on the page. If frames are being
          used, that top-most document will have a different URL and the test will succeed. If there are no frames, the top document and the current document
          are the same object and have the same location URL, indicating that frames are not being used.

          if (self != top)
          This is a more general version of the first condition. Instead of testing the document location, simply check to see if the current document is the same
          object as the top document on the page. If not, frames are being used.

          if (top.frames.length > 0)
          The top document has an object that contains all the frames in use on the page. Within that object, the length member keeps a count of how many
          frames are currently defined. If that count is greater than zero, frames are being used. It is also possible (and more elegant) to write this condition
          simply as top.frame.length. Since any non-zero value in JavaScript (like C) is equal to "true", a non-zero frame count returns a true value as well.

          if (top.frames[0].name != "name")
          This is a more specific test that only works if your page contains its own frames. In that case, you can test the name of a specific frame to see if it
          matches that frame in your document. You'll need to replace the 0 in the condition with the number of a suitable named frame in your document, and
          replace the "name" with the name of the frame. If the names don't match, your frames have been captured within another set of frames.

     Once you detect that they are in use, breaking free is easy. Simply set top.location to your document's URL. When this property is changed, the browser
     will reload the document into the top-level browser display, removing all the frames and replacing them with your document.

     Readers were split in setting top.location, relying on two principal methods:

     top.location = "URL";
     top.location = self.location;

     The first sets the location to an explicit URL which you must provide. The second uses the URL of the current page. I prefer the second method, since you
     need not ever update the URL if the page moves or changes its name. The first version will require ongoing maintenance as your site grows and evolves.

     The last piece to the puzzle is adding this snippet of code to your documents. We put this down with the onLoad attribute of the <BODY> tag. The JavaScript
     fragment assigned to this attribute is executed as the page is initially loaded by the browser. This means that the page can detect the presence of frames
     almost immediately, before your page is fully presented to the user. Since a change to the top.location property results in an immediate reload, you'll
     break free of the frames almost instantly.

     Collecting all my favorite pieces of the puzzle, I offer this version of the <body> tag to frame-proof your pages:

     <body onLoad="if (self != top) top.location = self.location">

     Add all the other attributes to it, of course, and give it a try in your pages. If it doesn't work, I'm sure to hear about it.




Thanks, that is exactly the solution I was looking for.
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.