![Firefox logo](https://assets-prod.sumo.prod.webservices.mozgcp.net/media/uploads/products/2020-04-14-08-36-13-8dda6f.png)
How to access parent window in javascript when popup is in a different domain
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)
선택된 해결법
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(...); }
};