2

I have a script that opens a popup in the same host. The popup is opened with window.open(url,name,settings) without assigning it to a variable.

I can't change the function that opens the popup and neither the code of the popup. But I can add extra Javascript code in the opener's load/ready event.

What I need is to know, from the opener, when the popup is open.
I know the name of the window, so if it's already open I can check it with:

var openedWin = window.open('', 'Selection');

But the problem is that when the window is not open, it tries to create a new window.

How can I implement a listener or something that let me check when a named popup window is opened?

Thank you!

Romualdo
  • 107
  • 8
  • https://stackoverflow.com/a/3030893/3684265 – imvain2 Aug 06 '20 at 17:40
  • @T.J.Crowder, then they also state: `I know the name of the window, so if it's already open I can check it with: var openedWin = window.open('', 'Selection');` So I guess the contradiction was what confused me. – imvain2 Aug 06 '20 at 17:45
  • 1
    @imvain2 Fair enough. It's not a contradiction, though. They're saying that they know the window name (`'Selection'`), not the name of a variable referring to the window. So they *could* get it by calling `var openedWin = window.open('', 'Selection');` **if** it were open (but if it isn't, that would open a new window, which presumably they don't want to do). :-) – T.J. Crowder Aug 06 '20 at 17:49

1 Answers1

1

I can only think of one way to do this, and it's ugly: Monkeypatch window.open in the opener's load/ready event you said you have access to:

var original = window.open;
window.open = function(url, windowName, windowFeatures) {
    // Chain to the original
    var wnd = original.call(window, url, windowName, windowFeatures);

    if (windowName === "Selection") {
        // Do whatever it is you want to do with `wnd`
    }

    return wnd;
};

If there's any other solution, use it; but if there isn't (and I don't think there is), this should work.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • Interesting approach. Yeah, not pretty, but interesting. The alternative I had was even uglier: To add another click event on the button that opens the popup, that waits one or two seconds (to make sure the popup is opened) and do the things I want. Thank you! – Romualdo Aug 07 '20 at 13:43
  • @Romualdo - LOL, now why didn't I think of that? :-) Happy coding! – T.J. Crowder Aug 07 '20 at 13:54