Update (2019): This article has been superseded by How to Prevent Your Website from being Placed in a Frame. Please read the latter instead.
Update (2009): If your website is hosted on an Apache web server, you might also be interested in using the frame blocking measures supported by modern browsers to prevent clickjacking. As mentioned there, any method using JavaScript to defeat frames is fraught with problems, hence browser makers have come up with a superior solution.
Are you fed up of having your website trapped in some other site's frame? This article provides a few methods whereby you can provide your visitors with a simple way to break out of the frame.
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-2024 by Christopher Heng. All rights reserved.
Get more free tips and articles like this,
on web design, promotion, revenue and scripting, from https://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 https://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 or distribute this article in whole or part, in any form.
It will appear on your page as:
How to Break Out of Frames with JavaScript (Frame BreakOut Script)