How to Disable Browser Back Button using JavaScript

The browser’s back button allows the user to redirect back to the previous page that accessing before the current page. If you want to restrict users to back the previous page on your web application, the browser back button needs to be disabled. The browser’s back navigation can be disabled with JavaScript. Use history.pushState() event and onpopstate property of the WindowEventHandlers to stop back navigation on browsers.

The following code snippet disables the browser back button using JavaScript.

  • Place this code into the web page where back navigation to be restricted.
window.history.pushState(null, null, window.location.href);
window.onpopstate = function () {
    window.history.go(1);
};

Leave a reply

keyboard_double_arrow_up