Thursday, April 23, 2009

Cross-browser javascript to disable browser's back button




Please visit my new Web Site WWW.Codedisplay.com



This is the most common problem for a developer to prevent the user to press the browser back button. One easy solution is you can open a new window which has a disabled back button. Another solution is you can remove the toolbar using javascript.

But most of the cases i think you cant apply above techniques. Thats why i want to share my experience that how we can resolve this problem. One thing you have to understand that there's no perfect way to keep the user from backing up, but we can force the user to go forwards again.

For few cases like after successful login, after a confirm checkout of an e-commerce site even after signout you don't want to user go back form your page. So this is mandatory for us to prevent the user to go to the previous page. To explain the script fuctionality lets i have a login.aspx page. When user successfully logged in then the system redirect the user to his transaction page transaction.aspx. Now from transaction.aspx page i dont want to go back to the login.aspx page. According to the example you have to put the below javascript code just under the head tag of login.aspx page.

<head runat="server"><title>login page</title>
<
script type="text/javascript">
setTimeout("window.history.forward(1)", 0);
window.onunload=function{return false;}
</script>
</
head >

Now run the login page. After login click on the browser back button & check what haapend. I want to share what is onunload event. In a easy way i can explain that onunload event is: it is called right before the browser navigates to a new URL. So i hope that everything is clear.


Script tested for the following browsers:
*** Internet Explorer
*** Opera
*** Mozilla Firefox
*** Google Chrome

For further reading:

http://www.hunlock.com/blogs/Mastering_The_Back_Button_With_Javascript

1 comments:

Rony John said...

danx..........
its working

Want to say something?
I WOULD BE DELIGHTED TO HEAR FROM YOU

Want To Search More?
Google Search on Internet
Subscribe RSS Subscribe RSS
Article Categories
  • Asp.net
  • Gridview
  • Javascript
  • AJAX
  • Sql server
  • XML
  • CSS
  • Free Web Site Templates
  • Free Desktop Wallpapers
  • TopOfBlogs
     
    Free ASP.NET articles,C#.NET,VB.NET tutorials and Examples,Ajax,SQL Server,Javascript,Jquery,XML,GridView Articles and code examples -- by Shawpnendu Bikash