Grow your CSS skills. Land your dream job.

Last updated on:

Break Out of iframe

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

That will generally work, but there is a small chance of failure in the case that window is overridden. Here's a couple of clever alternatives from Nathan Smith:


// Break out of an iframe, if someone shoves your site
// into one of those silly top-bar URL shortener things.
// Passing `this` and re-aliasing as `window` ensures
// that the window object hasn't been overwritten.
// Example:
//   var window = 'haha, punked!';
// Note: Probably unnecessary, but just for kicks.

(function(window) {
  if (window.location !== { = window.location;


// A more cryptic one-liner, to awe & impress.
// No need to protect `window` since `this` is
// immutable, and at the topmost level means
// `window` anyways. Here, we compare locations
// on the left side of the "&&" and execute the
// code in parenthesis if that condition is
// true (top location isn't iframe location).
// Otherwise, nothing happens. It's basically an
// if statement without wrapping curly brackets.
// Weird, I know. But pretty cool, right? :) !== this.location && ( = this.location);


Reference URL


  1. Permalink to comment#

    Is there any way to prevent it? From the parent.

    • Eru
      Permalink to comment#

      maybe if you try to dispatch the parent, because it is only used by HTMLIFrameElement

  2. The Fastest
    Permalink to comment#

    please, examples of blogs using an iframe

  3. Permalink to comment#

    Unable to get this to work on word press

  4. Scott
    Permalink to comment#

    Will this result in registering two visits if I’m using Google Analytics to track it?

    If so, is there an alternate method that will allow the breakout, but not register two visits?

  5. Miro
    Permalink to comment#

    This isn’t assured of working — setting the can be bypassed ( Instead, you should set use the option detailed directly above that wikipedia section — only setting visible when it is detected that you aren’t in an iframe.

  6. Dan
    Permalink to comment#

    Shouldn’t your first snippet be?:

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

    When I tried it without the ‘href’ part, it caused a redirect, rather than just breaking out of the iFrame.

    • Dan
      Permalink to comment#

      Sorry, ignore my last comment. It looks like it also does the reload with the “.href” added. Not sure why it appeared to do it without the reload when I first tried it with the “.href”! I should have done a bit more testing before posting my last post! Shame that it has to reload the page. Thanks for the info though.

Leave a Comment

Posting Code

  • Use Markdown, and it will escape the code for you, like `<div class="cool">`.
  • Use triple-backticks for blocks of code.
      <h1>multi-line block of code</h1>
      <span>be cool yo.</span>
  • Otherwise, escape your code, like <code>&lt;div class="cool"&gt;</code>. Markdown is just easier though.

Current ye@r *

*May or may not contain any actual "CSS" or "Tricks".