搜尋 Mozilla 技術支援網站

防止技術支援詐騙。我們絕對不會要求您撥打電話或發送簡訊,或是提供個人資訊。請用「回報濫用」功能回報可疑的行為。

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

};