Identifying Between Refresh And Close Browser Actions

前端 未结 13 1502
孤街浪徒
孤街浪徒 2020-11-22 15:29

When we refresh the page (F5, or icon in browser), it will first trigger ONUNLOAD event. When we close the browser (X on right top icon),It will trigger ONUNLOAD event. Now

13条回答
  •  面向向阳花
    2020-11-22 15:52

    Credit to https://www.anandkanatt.com/how-do-i-detect-browser-window-closed-refreshed/#comment-15892. I simplified it a little by using the opener itself to check. Tested in Chrome Version 78.0.3887.7.

    You may try this:

    • Add a refresh-close-detector.html file. Here's the sample code:
    
    
    
      
      
      
      Processing...
    
    
    
    
    
    
    
    • In the page you want to identifying between refresh and close browser actions, add an event listener to unload:
    window.addEventListener('unload', () => {
      open('refresh-close-detector.html', '', 'width=100,height=100');
    })
    

提交回复
热议问题