搜索 | 用户支持

防范以用户支持为名的诈骗。我们绝对不会要求您拨打电话或发送短信,及提供任何个人信息。请使用“举报滥用”选项报告涉及违规的行为。

Learn More

How to access parent window in javascript when popup is in a different domain

  • 1 个回答
  • 8 人有此问题
  • 135 次查看
  • 最后回复者为 Dyvik Chenna

more options

I have an html page in www.x.com domain that open a popup with window.open containing another page in www.y.com domain and i want these page to comunicate with window.postMessage, how can i access to parent page within the popup ? I tryed window.opener but it is undefined

I have an html page in www.x.com domain that open a popup with window.open containing another page in www.y.com domain and i want these page to comunicate with window.postMessage, how can i access to parent page within the popup ? I tryed window.opener but it is undefined

被采纳的解决方案

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(...);
 }

};

定位到答案原位置 👍 1

所有回复 (1)

more options

选择的解决方案

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(...);
 }

};