javascript 如何从跨站点弹出窗口访问window.opener?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8141417/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to get access from cross-site popup to window.opener?
提问by NiLL
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.
我正在制作一个小部件,我需要将父窗口重定向到某个 url,在弹出窗口中的特定事件之后,基于另一个域。怎么能做到这一点。
window.opener.location.replace(url);
回答by J. K.
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
但是,您可以通过此处描述的跨文档消息传递与另一个窗口进行通信: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.
您最多可以从弹出窗口向 opener 发送消息,并在 opener 中收听此类消息。然后开瓶器必须自己改变它的位置。
// popup:
window.opener.postMessage('replace your location', '*');
// opener:
window.onmessage = function (e) {
if (e.data === 'replace your location') {
window.location.replace(...);
}
};
回答by rkallensee
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 所建议的那样。