Apr 14, 2011

Disable Back Button in Browser using JavaScript


 While we are developing any web application then some times it's necessary to disable the back button effect from the browser. This is common in banking websites and other sites where security is major concern. User may hit the back button from the browser and forget to log out from the site.

There fore it's required to disable browser's back button.

So here is the code which will prevent user to go back on previous page.
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>New event</title>
</head>
<SCRIPT type="text/javascript">
window.history.forward();
function noBack() { window.history.forward(); }
</SCRIPT>
</HEAD>
<BODY onload="noBack();"
onpageshow="if (event.persisted) noBack();" onunload="">
<a href='page2.html' >Page 2</a>
</body>
</html>

Code for Page 1.


<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>New event</title>
</head>

</HEAD>
<BODY>
Page 2
</body>
</html>

Code for page 2.

Here I have given 2 pages page 1 and page 2. From page 1 I am going to page 2 using page 2 link. Now if I will click on page 2's browser back button it will go to page 1 but from page 1 it will again forward to page 2 so user can't go to the page 1 using back button.

Note : Above code is tested in IE 9.0 and Firefox 3.6.8


10 comments:

Anonymous

Gud JOb,,Thanks

-Arun Sunny

comment system

Very well written code. I just checked the above given code. It worked great for me. All credit goes to you. Thank a ton. Looking forward for more useful posts :)

web design company

It seems interesting to learn it from ur blog.Thank you dude !

Anonymous

thank you much her ​​article

Anonymous

Good Job dude, Thank you ... :)

Anonymous

hai

BloodSimple

This is great.. i've been searching this. can you give some sample in asp.net using VB or C#?
thanks..

Anonymous

Not Working ........ There is no such event "onpageshow" for body tag

TARESH PRAJAPATI

Good Work Dude It;s working......

jitesh

working fine........

Post a Comment

Author Profile

Total Pageviews

Categories

Followers

 
Top Programming   Sites Technology Top Blogs Technology blogs Technology Blogs

Sponsors