2

How to differentiate between the browser close and refresh event using JavaScript for CHROME?

I've tried below JavaScript code but it does not work for Chrome.

window.onbeforeunload = function (e) { 
           var e = e || window.event; 
           if(e.clientY < 0) { 
              aboutToClose(); 
              e.returnValue = ''; 
           }
           return ''; };
Robert Moskal
  • 21,737
  • 8
  • 62
  • 86
rupesh
  • 413
  • 9
  • 19
  • Perhaps [this question][1] [1]: http://stackoverflow.com/questions/26590092/browser-refresh-using-onbeforeunload-event will be helpfull. – Nicolas Castro Feb 23 '15 at 02:57
  • Do you mean when a tab is refreshed or a tab is closed? Or do you actually care if the entire browser is closed? – Damon Smith Feb 23 '15 at 04:44
  • 1
    @DamonSmith I meant was tab. When a tab is refreshed or closed same event get called i.e. "window.onbeforeunload". But how to differentiate whether it was "Refresh" or "Close" event? – rupesh Feb 25 '15 at 09:54
  • Ok, well, there's nothing in any browser API that will tell you that. What is it you want to do? Just record it for logging purposes? Or is there something you want your app to do differently if it was loaded again from a refresh? Because there are ways to do that. – Damon Smith Feb 26 '15 at 10:54
  • 1
    Apologies for reverting so late on this. Well I wish to implement different behavior for Refresh event and different behavior for close event. Can you please let me know the other ways as you mentioned to handle this? – rupesh Apr 20 '15 at 02:45
  • Does this answer your question? [Identifying Between Refresh And Close Browser Actions](https://stackoverflow.com/questions/568977/identifying-between-refresh-and-close-browser-actions) – Heretic Monkey May 06 '20 at 12:18

0 Answers0