How to get access from cross-site popup to window.opener?

前端 未结 2 883
借酒劲吻你
借酒劲吻你 2021-02-04 05:37

I\'ve making a widget and I need to redirect a parent window to certain url, after specific event in popup, whitch base on another domain. How a can do this.

win         


        
2条回答
  •  别那么骄傲
    2021-02-04 06:08

    You just cannot do that. Cross-site scripting is not allowed in most browsers.

    You can, however, communicate with the other window via cross-document messaging described here: https://developer.mozilla.org/en/DOM/window.postMessage

    The most you can to is to send a message from the popup to the opener and listen for such message in the opener. The opener then has to change its location on its own.

    // popup:
    window.opener.postMessage('replace your location', '*');
    
    // opener:
    window.onmessage = function (e) {
      if (e.data === 'replace your location') {
        window.location.replace(...);
      }
    };
    

提交回复
热议问题