const newWindow = window.open(
"",
"_blank",
"width=800,height=600,left=2000,top=0"
);
if (newWindow) {
const specificDivContent = document.getElementById(
`slide-${slideIndex}`
).innerHTML;
above is the code i am using to create a new tab and write an existing div onto that tab. i would like to call an api when the new window that is opened is refreshed or closed.
tried using these listeners below.
window.addEventListener("beforeunload", function(event) {
// Prevent the user from leaving the page.
console.log('tried closing the window')
event.preventDefault();
});
function checkWindowClosed() {
if (window.closed) {
console.log('window closed')
} else console.log('still open')
}
setInterval(checkWindowClosed, 1000);
2
Answers
want to add the listener to the newWindow you created using window.open()?
With a little error checking we’ll know if accessing the window is successful…