Solution to browser back button click event handling

By | December 10, 2013

In Javascript, 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 etc.

Normally, onbeforeunload event is used in order to handle browser back button functionality as follows:

But there is a problem that identical event occurs once a user clicks on refresh button of a browser. So, to grasp whether or not refresh button or back button is clicked, we will use the subsequent code.

Above code snippet works well in browsers aside from FireFox, In case of FireFox, we need to apply the following check.

So, consolidated code snippet will look as:

Above code snippet 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 which can not be handled using above code.
The Full JavaScript & ES6 Tutorial – (including ES7 & React)
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.
So, what’s the concept is…?
We will store the current page URL in session variable (say CurrentPageURL) on a page. When moved to another page, we will get that variable value and store in a second session variable (say PreviousPageURL) and update CurrentPageURL session variable with new page URL. ASP.NET straightforward code implementation is as follows:
On client side, when onbeforeunload event is fired, we will verify that whether or not Javascript document.referrer value is same as that of the session variable? If the value is same, means back button is clicked, so will act accordingly.
This solution works well regardless of the browser type and simple to implement. Server-side implementation in other languages is also straightforward. Please feel free to comment and suggest a better idea (if any).

You are developing a Students web form that includes the following HTML input field.
If a Student enters a value in the input field, then it must be a numeric value. You need to add validation to the input field. Which HTML should you use?

  • A. <input id=”txtValue” type-“text” pattern=”/#” />
  • B. <input id=”txtValue” type=”number” />
  • C. <input id=”txtValue” type=”integer” />
  • D. <input id=”txtValue” type=”text” required=”required”/>

For a complete online test and Practice Exams on Web Technologies, Click Here.

 Correct Answer: B

If you want to integrate Google Maps in a Website or Mobile application like Android, iOS and don’t know how to integrate and create programs in Google Maps by using Google Maps JavaScript APIs? you can find this complete Video Course really helpful: Click Here to Take the Video Course Now.

Using HTML5 and JavaScript, you will be:

  • able to setup Google Maps APIs
  • able to create Google Maps for devices
  • able to create Google Maps in a website
  • able to control the Maps UI events

Google Map JavaScript API


Top 10 Interview Questions and Answers Series:

Category: ASP.NET Browsers Internet Explorer Javascript Tags: , , ,

About Web Development

Imran Abdul Ghani is working as Software Developer(Senior) with extensive knowledge in Web development technologies especially C#, ASP.NET, MVC, WCF, Web API, ADO.NET Entity Framework, jQuery etc. He has several years of experience in designing/developing enterprise level applications. He is Microsoft Certified Solution Developer for .NET (MCSD.NET) since 2005. You can reach his blogging at www.webdevelopmenthelp.net, www.topwcftutorials.net, and www.sharepointfordummies.net.

5 thoughts on “Solution to browser back button click event handling

  1. Thirumala Reddy

    I am copy past the your code i got this exception “event is not defined ”
    In this line “event.currentTarget.performance.navigation.type”.
    i am using only html and java script

  2. Sridhar T

    how to handle in case of location.hash. if URL includes # symble, then it is working on second click not on first click with IE 8 and IE 9, CHROME, FIREFOX

Comments are closed.