1. Technology
Send to a Friend via Email

Frame Breakout

2. Selctive Frames Breakout

clr gif
More of this Feature

Frames breakout Force Reload

If you don't like the idea of your page appearing in someone else's frames then you have probably used the code on my page Frame Breakout On that page you discovered a simple one line JavaScript to unconditionally break your page out of frames.

That code is all very well if you never want your page to appear in any frames. You may however have a page that you normally want to display by itself but where you want it to be able to display in one or more specific framesets. A minor change to the code to break out of frames will allow you to change the link to the page to bypass the frames breakout when you specifically want your page to appear in the frameset. The JavaScript code that you need in the head section of your page to allow for this selective breakout is as follows:

<script type="text/javascript">
var args = location.search.substr(1).split("?");
if (top.location != self.location && args != "f")
top.location = self.location;
</script>

Let is suppose that your page is called page.htmindex.html and that therefore any links to your page will be coded like this.

<a href="index.html">link to page</a>

Any links coded like that from within a framed page will result in the Javascript code executing to reload the page without any frames - just the same as the earlier version of the code. This means that the page still breaks out of any frames that you don't specifically identify.

We do however now have a second way that we can link to the page where we tell the page not to break out of any frames that it may happen to be in. To do this we code the link like this:

<a href="index.html?f">link to page</a>

The extra piece on the end of the link tells the JavaScript code that this particular link to the page should not force the page to reload outside of frames but should instead keep the page inside of any frames that the calling page is already in.

We can of course also make use of this to code a reference to the page within a frameset definition itself using the following code:

<frame name="main" id="main" src="index.html?f" />

So this version of the page gives you complete control over when your pages will appear in frames because unless you specifically add the ?f to the end of the link the page will automatically break out of any frameset and since you will only specify the ?fm where the page is to be displayed in frames the index.html?f address will not appear in the address bar making it unlikely that others will think to include it when linking to your page.

Note that this page is copied from the "Ask Felgall" website with the permission of the copyright owner.

See More About

©2014 About.com. All rights reserved.