Handle back button of the browser

Virendradugar
Posted by in ASP.NET category on for Beginner level | Views : 26069 red flag
Rating: 3.67 out of 5  
 3 vote(s)

This article explains various techniques to disable / Handle back button of the browser.
Introduction
How many time, it has happened with you that when your client has asked you to that end-user should not be allowed to go back to previous page using browsers back button? But the actual problem is we don't have any control on the browser's back button. The “Back” browser button cannot be actually disabled by a web application as the browser security will not allow this. But yes there are number of workarounds using which we can achieve this functionality.

Solutions

Using JavaScript, we can forward the user to the same page if user presses the back button of the browser. See the code below :

 <script type="text/javascript" language="javascript">
    
    function GoBack()
    {
        window.history.forward();
    }
    </script>
Call this JavaScript function on the onload event of body tag.

<body onload="GoBack();">

This method will always redirect to the user on the current page itself.

For example, user is on page 1 and there is a link on Page 1 which takes user to Page 2. Now when user clicks on the link, browser redirects the control to Page 2. Now from page 2 if user press back button of the browser then user will be redirect to the page 2 itself.

window.history contains the collection of the page visited by the user for particular browser session. As an alternative of window.history.forward(), one can also use window.history.go(+1). As both are same.

But if we want to display web page expired warning on the click of back button as we normally see in all banking sites? Actually what happens is, when you press the back button, browser takes the page from cache and display it on screen. So if we want to show web page expired warning, we should not allow the browser to cache the page. We can achieve this via ASP.NET.

protected void Page_Load(object sender, EventArgs e)
{  
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
}

This code will not allow page to be cached. But one problem with this approach is that this will only work when there are some dynamic content in the page. i.e. change any drop down box value and then try to press back button.

Conclusion
Your comments are always appreciated. If you can find out better solution then this please let me also know.

Enjoy...

Page copy protected against web site content infringement by Copyscape

About the Author

Virendradugar
Full Name: Virendra Dugar
Member Level: Silver
Member Status: Member,MVP
Member Since: 8/11/2009 4:14:05 AM
Country: India

http://jquerybyexample.blogspot.com
Virendra Dugar is experienced Senior Software Developer with over 5 years of hands-on experience working with Microsoft .NET technology (ASP.NET, C#, VB.NET,SQL Server). He is always keen to learn new technology. He holds a Master's Degree in Computer Application & Information technology from Gujarat University in india.In free time, he loves to listen music, read books, play games and do blogging etc. Visit his blogs : http://jquerybyexample.blogspot.com

Login to vote for this post.

Comments or Responses

Posted by: Vuyiswamb on: 6/22/2012 | Points: 25
It does not work. i have attached an Example to prove that

Login to post response

Comment using Facebook(Author doesn't get notification)