Fed up of being trapped in some other site's frame even when you've left their site? This article provides a few methods whereby you can provide your visitors with a simple way to break out of the frame. It serves as a tutorial on its own right, but also an explanation of the code generated by thesitewizard.com's Free Frame Breakout JavaScript Wizard.
The traditional method is to put a link on your page that allows your visitors to click and open the existing URL in the window without the frame. Code for such a link might look like the following:
The target="_top" attribute causes the web browser to open the document in the
topmost window. This method has the disadvantage of requiring your visitors to actually read
your instructions and click the link.
You can also provide a way for them to break out of frames without having to take any action themselves. One way to do this is through a JavaScript function which you can put in your <head> section, like the following:
Alternatively, you can put the following in a file called (say) "scripts.js" and load it from every page. I do this at thesitewizard.com since it allows me to maintain one copy of the script even though it is loaded by every page on the site.
The code to include the script in your web page is:
It should be placed in the <head> section of your web page.
The function breakout_of_frame() checks to see if the URL of the topmost window is the same as the URL of the current window. If it is not the same, it sets it to be that of the current window, effectively loading the URL into the top window and removing the frames.
To use the above function automatically, you will have to call the script in the "body" tag:
and voila! If your visitors have JavaScript enabled, when they enter your site, they'd be automatically rid of the irritating frame.
If you prefer to give your visitors the choice of whether to continue in a frame or to break out of it, you can put a button on your page that users can click to break out of the frame instead of using the "onload" attribute in your <body> tag. The code for the button is as follows:
Such code may be useful if the frames are actually your own, and you want to allow your visitors to have a "No Frames" version of your pages without having to bother with specifically coding for it.
Happy scripting!
Copyright 2000-2002 by Christopher Heng. All rights reserved.
Get more free tips and articles like this,
on web design, promotion, revenue and scripting, from http://www.thesitewizard.com/
Do you find this article useful? You can learn of new articles and scripts that are published on thesitewizard.com by subscribing to the RSS feed. Simply point your RSS feed reader or a browser that supports RSS feeds at http://www.thesitewizard.com/thesitewizard.xml. You can read more about how to subscribe to RSS site feeds from my RSS FAQ.
This article is copyrighted. Please do not reproduce this article in whole or part, in any form, without obtaining my written permission.
It will appear on your page as:
How to Break Out of Frames with JavaScript (Frame BreakOut Script)