2

I am trying to open a new window with a URL for oAuth, however I cannot add any event listener.

const wind = window.open(msg.data.url);
console.log(wind);
wind.onload = function () {
    wind.onpopstate = function (e) {
        console.log('pop', e);
    };
};

It just does nothing. However it turned out that window.open() has given me a not full window object. And that is all what I have got. How do I add event listener for that?

1

Timisorean
  • 1,388
  • 7
  • 20
  • 30
Sergey
  • 7,184
  • 13
  • 42
  • 85

1 Answers1

2

According to the MDN web docs regarding the window.open() function:

The returned Window reference can be used to access properties and methods of the new window as long as it complies with Same-origin policy security requirements.

That means if you call window.open("/questions") in a terminal on this webpage, you get a full window object, but if you call window.open("https://google.com"), it returns only a simplified object on which you will not be able to add event listeners. This is to prevent cross-origin attacks. You may, however, transfer data to the new window object via Window.postMessage(), if the new window is listening for that type of event. See here for more information on that.

Robb216
  • 554
  • 4
  • 10
  • But what happens when the window is redirected to my origin? I'm trying to open page with OAuth which redirects back to my page then. But I can't do it in main window due to work with Angular and websocket connection established. – Sergey Jan 29 '19 at 14:29
  • _“But what happens when the window is redirected to my origin?”_ - well, then you are in control of the contents inside the popup window again - so you could for example echo JS code, that performs certain functionality … If you only need to close the window, that might work directly; the reference to the opener is probably lost due to the cross-domain redirect though. In that case, you might need to communicate with the parent window using postMessage, to trigger what else needs doing from there. – 04FS Jan 29 '19 at 14:41