1

Is there any way to differentiate the events - reload and close the window? As $window.onbeforeunload works in both cases and I have not found any difference in both the events.

In my case, I have to call different functions on a different event.

So, please suggest any solution or alternative.

David Walschots
  • 12,279
  • 5
  • 36
  • 59
Aayush
  • 31
  • 5
  • 1
    Possible duplicate of [Identifying Between Refresh And Close Browser Actions](https://stackoverflow.com/questions/568977/identifying-between-refresh-and-close-browser-actions) – Teemu Apr 19 '18 at 10:51

1 Answers1

0

There is a hack that I found with Javascript that should work for most browsers.

How this works is the code is using the window.opener property. It is checking if an invalid property exists in an object.

  • So, if a parent window exists, then the return will be 'undefined'.
  • If a parent window does not exist, an exception will be raised which we will catch. This exception will tell us that the parent no longer exists, thus we know that the parent got closed.

Here is the code:

  function detectRefresh(){
     try
     {
       if(window.opener.title == undefined){
          isRefresh = true;
          document.write('Window was refreshed!');
       }
     }
     catch(err)
     {
        isRefresh = false;
        document.write('Window was closed!');
     } 
   }
James Drinkard
  • 15,342
  • 16
  • 114
  • 137