简体   繁体   English

如何从跨站点弹出窗口访问window.opener?

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

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.我正在制作一个小部件,我需要将父窗口重定向到某个 url,在弹出窗口中的特定事件之后,基于另一个域。 How a can do this.怎么能做到这一点。

window.opener.location.replace(url);

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-US/docs/Web/API/Window/postMessage但是,您可以通过此处描述的跨文档消息传递与另一个窗口进行通信: https : //developer.mozilla.org/en-US/docs/Web/API/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.您最多可以从弹出窗口向 opener 发送消息,并在 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(...);
  }
};

In certain situations it's possible to do that, but only with different subdomains, not completely different domains.在某些情况下,可以这样做,但仅限于不同的子域,而不是完全不同的域。 See Cross site scripting on the same domain, different sub domains .请参阅同一域、不同子域上的跨站点脚本

But since postMessage() is widely available in current browsers, you should always prefer postMessage() , as @ian-kuca suggests.但由于postMessage()在当前浏览器中广泛可用,你应该总是更喜欢postMessage() ,正如@ian-kuca 所建议的那样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM