Need to break out of iframe after content in iframe is submitted

I believe you want this

if(this != top){
  top.location.href = this.location.href;
}

To break out

It might need the document reference too... I'm not at a computer to check.

if(this != top){
  top.document.location.href = this.document.location.href;
}

What worked for me was:

(function () {
    'use strict';
    console.log('window.top.location', window.top.location);
    console.log('window.location', window.location);

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

Inspired by https://css-tricks.com/snippets/javascript/break-out-of-iframe/


Chris Coyier at css-tricks.com has a nice succinct explanation of how to do this.

One way is a little clearer to the observer (as much as production Javascript code can ever said to be "clear"):

(function(window) {
  if (window.location !== window.top.location) {
    window.top.location = window.location;
  }
})(this);

The other is much shorter, but also trickier and less obvious:

this.top.location !== this.location && (this.top.location = this.location);

Again, credit where credit is due: I didn't write these snippets, I'm just passing them along because they answer the question.