How to stop the browser back button in JS.

BharteeSahareTechRoR
2 min readSep 8, 2023

In the world of web development, JavaScript is a powerful tool that allows developers to create interactive and dynamic web applications. One common requirement in web development is controlling the behavior of the browser’s back button.

In this blog post, we’ll dive deep into a JavaScript code snippet that disables the browser’s back button and explain how it works step by step.

Let’s take a look at the code:

function DisableBackButton(){
window.history.forward()
}
DisableBackButton();
window.onload = DisableBackButton;
window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }
window.onload = function() {void(0)}

Understand the Code:

1. Function Definition:

The code begins with the definition of a JavaScript function named `DisableBackButton`. This function contains a single line of code:

window.history.forward()

This line is responsible for navigating forward in the browser’s history, effectively disabling the back button.

2. Initial Execution:

After defining the `DisableBackButton` function, it is immediately called using `DisableBackButton();`. This ensures the process is executed as soon as the script is loaded.

3. Window `onload` Event:

The next line of code sets an event handler for the `window.onload` event:

window.onload = DisableBackButton;

This line specifies that the `DisableBackButton` function should be executed again when the page finishes loading. This ensures that even if a user attempts to go back to a previous page, the forward navigation will be triggered again.

4. Window `onpageshow` Event:

The code also sets an event handler for the `window.onpageshow` event:

window.onpageshow = function(evt) { if (evt.persisted) DisableBackButton() }

The `onpageshow` event is triggered when the user navigates to a page from the browser’s session history. The provided function checks if the event has been persisted (meaning the user navigated using the back/forward buttons or a similar method). If it has been persisted, the `DisableBackButton` function is called again to prevent the user from going back further in history.

5. Window `onload` Event (Again):

The last line of code sets the `window.onload` event to an anonymous function:

 window.onload = function() {void(0)}

This line effectively overwrites the previous `window.onload` handler. However, this anonymous function doesn’t perform any actions (`void(0)`), essentially preventing the previous `DisableBackButton` function from being executed again.

In summary, this JavaScript code snippet is designed to disable the browser’s back button by navigating forward in the history whenever the page is loaded, preventing users from going back to previous pages. Additionally, it handles the `onpageshow` event to ensure the back button remains disabled when users navigate using the browser’s history.

Happy deploying! 🚀 Please follow for more updates BharteeTechRoR

--

--

BharteeSahareTechRoR

Ruby on Rails Developer || React Js || Rspec || Node Js