Break Out of Frames Using JavaScript

By  on  

I thought frames went out of style a decade ago but apparently everyone feels the need to duplicate the Digg Bar. I don't trust these framed services so I choose to use a JavaScript snippet that prevents my site from being in someone else's frame.

The JavaScript

if (top.location != self.location) {
	top.location = self.location;
}

Don't let your site be framed in! Use this JavaScript snippet!

Recent Features

  • By
    5 Ways that CSS and JavaScript Interact That You May Not Know About

    CSS and JavaScript:  the lines seemingly get blurred by each browser release.  They have always done a very different job but in the end they are both front-end technologies so they need do need to work closely.  We have our .js files and our .css, but...

  • By
    Camera and Video Control with HTML5

    Client-side APIs on mobile and desktop devices are quickly providing the same APIs.  Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop.  One of those APIs is the getUserMedia API...

Incredible Demos

  • By
    Implement jQuery’s hover() Method in MooTools

    jQuery offers a quick event shortcut method called hover that accepts two functions that represent mouseover and mouseout actions. Here's how to implement that for MooTools Elements. The MooTools JavaScript We implement hover() which accepts to functions; one will be called on mouseenter and the other...

  • By
    Using MooTools to Instruct Google Analytics to Track Outbound Links

    Google Analytics provides a wealth of information about who's coming to your website. One of the most important statistics the service provides is the referrer statistic -- you've gotta know who's sending people to your website, right? What about where you send others though?

Discussion

  1. haha, I really like this. I agree with your thoughts on frames.

  2. I thought I’d seen that for the last time last millennium! Although I also agree with the nasty framed services – so might have to join in and add it to my site too :) Obvious, but very easy to not think of

  3. This is the very same basic technique for preventing clickjacking. Basic, because it doesn’t work in IE, if your site is framed with security=”restricted”

  4. As seen on The Good Parts… shouldn’t you use !== instead of != for comparisons like this one?

    d.

  5. haha!
    this is first javascript that i learn when i was baby :D

  6. David,

    i agree. I saw a few articles ranting about the Digg bar when it came out and one of them had this same solution – so I added it. Works great!

    Just had an idea though – what if you could recognize where the bar was coming from (like Digg) and get rid of the bar but add some little area to your page that recognizes the user and the functionality they might want. For Digg, just add a little Digg this button or whatever the Digg bar offers. Facebook, Hootsuite, a few others that I know of.

    It would be similar to the WP Greet Box Plugin that gives a quick message depending on where visitors come from.

    This way you lose the bar, but keep the sharing aspects and make it look like it fits your site.

    -Marty

  7. Ben

    I like frames sometimes. I think the Diggbar is really useful. However frames can be over used like you say.

  8. I use this on one of my sites. It works OK and you need just 2 lines of javascript. Personaly I hate frames. This was popular years years ago.

  9. Wow, haven’t seen this piece of code in 8-10 years, when using frame was the norm.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!