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?