Industry guru Dave Taylor answers free tech support questions about a wide variety of business and technical topics, including blogging, Google AdSense, MySpace, Sony PSP, Apple iPod, Mp3 players, management, Linux, SEO, Mac OS X, Facebook, Twitter, LinkedIn and Microsoft Windows.

How do I use JavaScript to break out of frames?

I'm really upset. I was just visiting a competitors Web site and found out that they had links to my FAQ pages, but when you clicked on the links, their "frame" neatly wrapped my content, which made it seem like it was their content, not mine. That stinks, and I want to know if there's a way that I can kill or prevent this from happening to my Web pages?


Dave's Answer:

There is a pretty simple solution, fortunately, because the original Web designers recognized that while having multiple pages displayed as one "montage" or "frameset" offered lots of cool design features, it would also make sites susceptible to unauthorized framing with the same HTML sequence.

To create a frameset, you use a combination of <frameset> and <frame> and there are plenty of examples of how to accomplish that online and in my popular book Creating Cool Web Sites, but let's look at the other half of the coin, what you're asking.

The trick is to delve into JavaScript, the scripting language of Web pages. Don't worry, though, what we're doing isn't that complex, and I'm sure you can handle it!

Open up one of your Web pages and all you need to do is drop in a small JavaScript snippet surrounded by <script language="javascript"> and </script>, like this:

<script language="javascript" type="text/javascript">
if (top.location != self.location) {
    top.location.replace(self.location)
}
</script>

That's all there is to it. Now when that page is included in a frames-based layout, it will automatically "bust through" all the frames and force itself to be the topmost window. Hope that helps out!

Thanks to Dori Smith of JavaScript World.com for her tip on this splendidly efficient solution.



Help others find this article at Del.icio.us, Digg, Netscape, Reddit, and Simpy.

Subscribe!

Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader.

Comments

This is great. I've seen this solution before. My question is: What about the three* people out there who have javascript disabled? Is there a non-java solution or are we just happy that we're hitting 97%+* of all web users?

* I don't have real numbers. I pulled these out of the air.

Posted by: Dustin L. at August 10, 2006 1:57 PM

Dustin, I honestly don't know if you can break out of frames if someone has JavaScript disabled. Perhaps someone else can pop up with an answer for this?

Posted by: Dave Taylor at August 11, 2006 8:16 AM

I don't think you can "break out of frames" without some scripting support in the client's browser.

However, you may be able to use server side includes (basically, running scripts on the machine hosting your site, rather than in the client's browser) and HTTP_REFERRER to replace the content of the page if the referrer is the competitor's site. (I've seen people do this with images on their sites, and the image that appears on the other site's page usually has some message about "an attempt to steal bandwidth".)

Another possibility is to simply rename the page on your site, and replace the old page with one that explains what happened. Obviously, people who bookmarked your page will need to update their bookmarks, but people seeing your file on the competitor's site will be notified as to what's going on as well.

Posted by: Ken at August 11, 2006 11:02 AM

Thanks for the code, its great for my blog.

ravs.us

Posted by: Ravi Gupta at December 26, 2006 12:18 AM

I have a lot to say, but ...
Starbucks coffee cup I have a lot to say, and questions of my own for that matter, but most of all I'd like to say thank you for all your efforts on this Web site by buying you a chai!

I do have a comment, now that you mention it!









Remember personal info?


Please note that I will never send you any unsolicited commercial email. Ever.

While I'm at it, please note that by submitting a question or comment you're agreeing to my terms of service, which are: you relinquish any subsequent rights of ownership to your material by submitting it on this site.









Search
Find just the answers you seek from among our 1700+ free tech support articles by using our Lijit search engine.


Help!





Subscribe to
Ask Dave Taylor!

Add to Google Reader
Add to My Yahoo!
Subscribe in NewsGator Online

RDF   XML

Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.


Recent Entries
Join the List!
Join my author info mailing list, where you'll learn about my upcoming books, speaking gigs, and more!


Book Links
© 2002 - 2008 by Dave Taylor. All Rights Reserved.

Note: This web site is for the purpose of disseminating information for educational purposes, free of charge, for the benefit of all visitors. We take great care to provide quality information. However, we do not guarantee, and accept no legal liability whatsoever arising from or connected to, the accuracy, reliability, currency or completeness of any material contained on this web site or on any linked site.

[whiteboard marker tray]