简体   繁体   中英

"beforeunload" event not firing on Google Chrome for popup window

I need to run some code when a second window is closed by the user:

var win = window.open("chat-box", null, "height=600,width=1300,status=yes,toolbar=no,menubar=no,location=no");  

if(win != null)
{
    win.addEventListener("beforeunload", function()
    {
        console.log("It closed");
    });
}

It works in firefox, but not Google Chrome.

Any ideas on why or how to fix it?

An approach I've used in the past is an interval timer to check for win.closed

if(win){
   let timer = setInterval(()=>{
       if(win.closed){
          console.log('It closed')
          clearInterval(timer)
       }
   }, 1000);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM