Click here to Skip to main content
14,920,012 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am having back button in the application itself. While clicking the browser back button it will take me to the wrong pages. So i need to disable the browser back button.
Kindly give me some suggestion to my issue.

What I have tried:

I have tried the following code. But it just refresh the current url.

$(document).ready(function () {
window.history.forward(1);
});
Posted
Updated 30-Jun-18 1:59am
Comments
F-ES Sitecore 28-Jun-18 4:28am
   
After 20 years people are still asking this question? Do basic research before asking a question such as using google. This is literally the most frequently asked question ever.

There are numerous reasons why disabling the back button will not really work. Your best bet is to warn the user:

window.onbeforeunload = function() { return "Your work will be lost."; };


This page does list a number of ways you could try to disable the back button, but none are guaranteed:

http://www.irt.org/script/311.htm
   
v2
Quote:
So i need to disable the browser back button.

Short answer: you don't.
The back button is part of client side functionalities and can't hijack it for security reasons. I guess you have to find another way to do what you want.
   
<html>
<head>

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.10.2/jquery-ui.min.js">
</script>
</head>
<body>
<a href="Page2.htm">Click Here...</a>
</body>
<script>
$(document).ready(function() {
function disableBack() { window.history.forward() }

window.onload = disableBack();
window.onpageshow = function(evt) { if (evt.persisted) disableBack() }
});
</script>
</html>
   
Comments
CHill60 3-Jul-18 7:27am
   
And what if the user (sensibly) has scripting disabled?
Besides, the history.forward option was already offered in Solution 1

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900