0

I would like to detect on page load if the targetwindow is open. the targetwindow gets opened from the main window. However if i refresh the main window the connection to the targetwindow gets lost.

when i click the button in the mainpage the targetwindow is opened, i then check every second if the targetwindow is open or closed and do stuff that i load from localStorage

function checkWindow(){

var windowObjRef = window.open('_external', 'targetWindow',
`toolbar=no,
    location=0,
    status=no,
    menubar=no,
    scrollbars=yes,
    resizable=yes,
    width=500px,
    height=800px`
);

    var checkExternal;

    checkExternal = setInterval(function () {

            if (windowObjRef.closed) {

                clearInterval(checkExternal);

                localStorage.removeItem('saved');

            }

            else {

                var saved = localStorage.getItem('saved');

                if (saved === 'true') {

                    localStorage.setItem('saved', false);

                    saved = false;

                    // do some other stuff here

                }

            }

    }, 1000);   

}

this all works fine until i refresh the main page where the button is. Then the link of course seems to missing since the interval is not running with the reference windowObjRef. I don't want the targetwindow to be opened automatically on page load as well. It needs to be the button click. How can i detect in javascript if the targetwindow is actually open or closed?

Ewald Bos
  • 1,560
  • 1
  • 20
  • 33
  • Does https://stackoverflow.com/questions/6264907/check-if-a-popup-window-is-closed answer your question? – Reporter Dec 09 '20 at 12:26
  • no not really since i just want to know if the targetwindow is open or closed, if it's closed fine, no action. However if it's open then i need to call the interval loop again – Ewald Bos Dec 09 '20 at 14:44
  • did find a solution that works but not as i want. https://stackoverflow.com/questions/24727036/closing-child-popup-window-when-parent-window-is-closed/24727281 in this case i just close the targetwindow and the user needs to open it again so the link is established. But i would prefer to just detect if the targetwindow is open or not – Ewald Bos Dec 09 '20 at 14:55

0 Answers0