NiLL
NiLL

Reputation: 13843

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. How a can do this.

window.opener.location.replace(url);

Upvotes: 18

Views: 30279

Answers (2)

J. K.
J. K.

Reputation: 8368

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

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

Upvotes: 45

rkallensee
rkallensee

Reputation: 2024

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.

Upvotes: 0

Related Questions