
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? 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 Stumble Upon
Categorized:
CGI Scripts and Web Site Programming
,
HTML and CSS
(Article 6721)
Tagged: frames, framesets, html layout, javascript Previous: How do I delete a friend from MySpace? Next: How do I organize my Hotmail folders? Subscribe!
Never miss another useful Q&A article again! Subscribe to AskDaveTaylor with Google Reader. 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 PMDustin, 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 AMI 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 AMThanks for the code, its great for my blog. ravs.us Posted by: Ravi Gupta at December 26, 2006 12:18 AMGrrrrrrrrrrrrreat code! Thanks! Posted by: Luiz at January 24, 2009 5:05 AMHi! This snippet is very useful, but what if I want for a certain site not to be frame breaked? I mean, I want to allow only one site to frame mine, all other should be break. How can I do? Thanks in avance. I have a lot to say, but ...
I do have a comment, now that you mention it!
|
![]()
Search
Find just the answers you seek from among our 2000+ free tech support articles by using our Lijit search engine.
Help!
Subscribe to
Ask Dave Taylor!
Free Updates!
Sign up and get free weekly updates and special offers on books, seminars, workshops and more.
Articles and Reviews
Auctions and Online Shopping Blogs and RSS Feeds Building Web site traffic Business and Management Cell Phones and Mobile Phones CGI Scripts and Web Site Programming Computer and Internet Basics d) None of the Above HTML and CSS Industry News and Trade Shows Mac OS X Help MySpace, Facebook, Twitter and Social Network Help Pay Per Click (PPC) Search Engine Optimization Shell Script Programming Sony PSP, MP3 Players, Etc. The Writing Business Unix and Linux Help Video Game Tips and Help Windows Help
Recent Entries
Join the List!
Book Links
|