Solution to Browser Back Button Click Event Handling in JavaScript

Introduction

 
In JavaScript, an onbeforeunload event is fired when the page is about to unload and there can be multiple reasons for this unload. For instance, back or forward or refresh button is clicked or a link on the page is clicked and so on. 
 
Normally, the onbeforeunload event is used for handling browser back button functionality as follows:
  1. <body onbeforeunload="HandleBackFunctionality()">  
  2.   
  3.  function HandleBackFunctionality()  
  4.  {  
  5.        if(window.event) //Internet Explorer  
  6.       {  
  7.            alert("Browser back button is clicked on Internet Explorer...");  
  8.        }  
  9.        else //Other browsers for example Chrome  
  10.        {  
  11.            alert("Browser back button is clicked on other browser...");  
  12.        }  
  13.  } 
But there is a problem that identical events can occur once a user clicks on the refresh button of a browser. So, to grasp whether the refresh button or back button is clicked, we will use the subsequent code.
  1. if(window.event.clientX < 40 && window.event.clientY < 0)  
  2.  {  
  3.       alert("Browser back button is clicked...");  
  4.  }  
  5.  else  
  6.  {  
  7.       alert("Browser refresh button is clicked...");  
  8.  } 
The snippet of code above works well in browsers other than Firefox. For FireFox we need to apply the following check.
  1. if (event.currentTarget.performance.navigation.type == 1)  
  2.  {  
  3.       alert("Browser refresh button is clicked...");  
  4.  }  
  5.  if(event.currentTarget.performance.navigation.type == 2)  
  6.  {  
  7.        alert("Browser back button is clicked...");  
  8.  } 
So, the consolidated code snippet will look as:
  1. function HandleBackFunctionality()  
  2.  {  
  3.      if(window.event)  
  4.     {  
  5.           if(window.event.clientX < 40 && window.event.clientY < 0)  
  6.          {  
  7.              alert("Browser back button is clicked...");  
  8.          }  
  9.          else  
  10.          {  
  11.              alert("Browser refresh button is clicked...");  
  12.          }  
  13.      }  
  14.      else  
  15.      {  
  16.           if(event.currentTarget.performance.navigation.type == 1)  
  17.          {  
  18.               alert("Browser refresh button is clicked...");  
  19.          }  
  20.          if(event.currentTarget.performance.navigation.type == 2)  
  21.         {  
  22.               alert("Browser back button is clicked...");  
  23.         }  
  24.      }  
  25.  } 
The snippet of code above is beneficial in many scenarios, however, there are some issues with it. For instance, navigation can be done using keyboard keys and refresh can also be done using F5 or CTRL+R that cannot be handled using the code above.
 
In order to handle back button functionality, we need to come up with a solution that requires server-side effort together with client-side JavaScript code.
 
The concept is...
 
We will store the current page URL in a session variable (say CurrentPageURL) on a page. When moved to another page, we will get that variable value and store it in a second session variable (say PreviousPageURL) and update the CurrentPageURL session variable with a new page URL. The ASP.NET straightforward code implementation is as follows:
  1. //ASP.NET/C# code  
  2.  if (Session["CurrentPageURL"] != null)  
  3.  {  
  4.         Session["PreviousPageURL"] = Session["CurrentPageURL"];  
  5.  }  
  6.  Session["CurrentPageURL"] = Request.Url; 
On the client-side, when the onbeforeunload event is fired, we will verify whether the JavaScript document. referrer value is the same as that of the session variable. If the value is the same, in other words, the back button is clicked, then it will act accordingly.
  1. function HandleBackFunctionality()  
  2.  {  
  3.         var previousPageURL = "<%= Session["PreviousPageURL"] %>";  
  4.   
  5.         if (document.referrer == previousPageURL)  
  6.         {  
  7.                 alert("Its a back button click...");  
  8.                 //Specific code here...  
  9.         }  
  10.  }
This solution works well regardless of the browser type and is simple to implement. The server-side implementation in other languages is also straightforward. Please feel free to comment and suggest a better idea (if any).
 
Blog: http://www.webdevelopmenthelp.net/