Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Force Page to load in _top

Posted on 1997-05-02
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.
Question by:willman
  • 4
  • 4

Expert Comment

ID: 1836186
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.

Author Comment

ID: 1836187
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.

Expert Comment

ID: 1836188
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';

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!


Author Comment

ID: 1836189
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.

Expert Comment

ID: 1836190
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.

Author Comment

ID: 1836191
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.

Accepted Solution

salt032897 earned 400 total points
ID: 1836192
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.  

Expert Comment

ID: 1836193
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.



Author Comment

ID: 1836194
Thanks, that is exactly the solution I was looking for.

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
The first step to building an amazing About page is to figure out what you want the page to say about your company. You then must grab the attention of the reader, boast a bit, tell a story and let others brag about you. With a little bit of thought…
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question