How can I stop the browser back button using JavaScript?
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
Trying to disable the browser's back button
FOA, Thanks everyone for your answers.
Finally below code gave me the solution:
history.pushState(null, null, window.location.href);
history.back();
window.onpopstate = () => history.forward();
Disable browsers' back button, completely
Based on your post it sounds like your only issue is disabling the backspace button from allowing the user to go back.
Here's what I do for that using jquery. Still allows backspace to work inside enabled text editing inputs, where it should.
// Prevent the backspace key from navigating back.
$(document).unbind('keydown').bind('keydown', function (event) {
var doPrevent = false;
if (event.keyCode === 8) {
var d = event.srcElement || event.target;
if ((d.tagName.toUpperCase() === 'INPUT' && (d.type.toUpperCase() === 'TEXT' ||
d.type.toUpperCase() === 'PASSWORD' ||
d.type.toUpperCase() === 'FILE')) ||
d.tagName.toUpperCase() === 'TEXTAREA') {
doPrevent = d.readOnly || d.disabled;
}
else {
doPrevent = true;
}
}
if (doPrevent) {
event.preventDefault();
}
});
Disable browser back button for one page application
I work in AngularJS building a Single Page App and I wanted to disable or prevent the back button of the browser, because my app has buttons and anchors for all navegation into my app.
I searched and test many codes, and this is the easy to prevent the back button of browsers and the following code worked for me.
When the history detects the first history.back()window.onpopstate = function (e) {
window.history.forward(1); }
is executed, then after this moment any back or forward in history are detected for onpopstate event.window.onpopstate
How to completly disable back button of a browser while taking online exam?
Try this script
<script>
window.location.hash="no-back-button";
window.location.hash="Again-No-back-button";//again because google chrome don't insert first hash into history
window.onhashchange=function(){window.location.hash="no-back-button";}
</script>
Also refer this link. This link contain demo & Download code
Related Topics
Passing Data to a Jquery UI Dialog
Single Quote Escape in JavaScript Function Parameters
How Is a JavaScript Hash Map Implemented
How to Send the "&" (Ampersand) Character via Ajax
Get Selected Value/Text from Select on Change
Dynamic Require in Requirejs, Getting "Module Name Has Not Been Loaded Yet for Context" Error
Does Use of Anonymous Functions Affect Performance
Viewing All the Timeouts/Intervals in JavaScript
Can Promises Have Multiple Arguments to Onfulfilled
Handling Key-Press Events (F1-F12) Using JavaScript and Jquery, Cross-Browser
Select a Complete Table with JavaScript (To Be Copied to Clipboard)
Do We Need Semicolon at the End
Angularjs - Cancel Route Change Event
Cross-Browser Bookmark/Add to Favorites JavaScript
How to Catch Jquery $.Getjson (Or $.Ajax with Datatype Set to 'JSONp') Error When Using JSONp
How to Show a Spinner While Loading an Image via JavaScript