Selective Frame Breakout

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 Don't Like Frames? 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 ="?");
if (top.location != self.location && args != "fm") top.location = self.location;

Let is suppose that your page is called index.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?fm">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?fm" />

So this version of the page gives you complete control over when your pages will appear in frames because unless you specifically add the ?fm 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?fm address will not appear in the address bar making it unlikely that others will think to include it when linking to your page.


This article written by Stephen Chapman, Felgall Pty Ltd.

go to top

FaceBook Follow
Twitter Follow