How to disable browser back button using Javascript

Last updated on February 12th, 2022 at 07:57 pm

Click to rate this tutorial!
[Total: 72 Average: 4.1]

Multiple methods to disable back button in browser using Javascript

Hello webmasters, I am going to share with you a quick tutorial on how to disable browser back button, this will work fine in Mozilla, IE , chrome and all other famous browsers.

With the use of a simple javascript we can force the webpage to go forward once the back button is being triggered. This script will be very powerful when combined with database operations where a web developer doesn’t want users to use back button in browser.

For example when a Bank Transaction has taken place in the previous page.

This one works with Mozilla Firefox

<body onUnload="noBack()">
<script>
function noBack()
{
window.history.forward(1);
}
</script>

You can also use the below script, but i have noticed that it wont work in some browsers especially Mozilla v 3.5

if(history.length>0)
{
history.go(+1)
}

Above scripts may not be compatible with all browsers.

This script works on Chrome and Firefox

<script type = "text/javascript" > 
history.pushState(null, null, location.href); 
history.back(); 
history.forward(); 
window.onpopstate = function () { history.go(1); };
 </script>

history.pushState() : Adds an entry to the browser’s history
history.back() : Make browser to move back one page
history.forward() : Make browser to move forward one page

More details on history interface can be found here https://developer.mozilla.org/en-US/docs/Web/API/History

Demo

Click to rate this tutorial!
[Total: 72 Average: 4.1]

Leave a Reply

Your email address will not be published.