How does JavaScript listen for page closure (non-refresh)?

< H2 > as mentioned above, we want to listen to the closing event of the page, but we will respond if we find that refreshing or closing will happen. < / H2 >

the following is the judgment code on the network, but it doesn"t seem to work. "Refresh or leave" will be printed all the time

.
window.onbeforeunload = function () {
    alert("");
    //  , ,  method
    // setTimeout(onunloadcancel, 3);
    if (event.clientX <= 0 && event.clientY < 0) {
      console.log("");
    }
    else {
      console.log("");
    }
    return "?";
  }

does JavaScript provide any solutions?

Jun.02,2021

refreshing the page is equivalent to closing and then opening it. It seems that you can't just listen for closed events. onunload doesn't work either. See if this helps you: http://udn.yyuap.com/forum.ph.

Menu