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

后端 未结 3 463
长发绾君心
长发绾君心 2020-12-07 01:10

Ok, I am using an iframe on a page. The content within the iframe I have no control over and is being utilized with an Adobe Flash program.

But once the form is sum

相关标签:
3条回答
  • 2020-12-07 01:34

    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/

    0 讨论(0)
  • 2020-12-07 01:42

    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;
    }
    
    0 讨论(0)
  • 2020-12-07 01:57

    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.

    0 讨论(0)
提交回复
热议问题