Scriptbaker
SCRIPTBAKERAI & Software Engineering
Engineering

Disable Browser's Back button

Learn why you can’t truly disable the browser Back button using JavaScript and how to properly manage app state using the History API, popstate, and bfcache instead.

· 5 min read · By Tahir Yasin

Sometimes web developers want to control what happens when a user clicks the browser's Back button. This can be useful in specific application flows, such as preventing users from accidentally returning to an outdated state after completing a transaction or handling navigation inside a single-page application.

One commonly shared JavaScript approach is to use the browser's History API:

function disableBackButton() {    window.history.forward();}setTimeout("disableBackButton()", 0);

How Does This Code Work?

The history.forward() method tells the browser to move one step forward in the current session history. It is essentially equivalent to calling history.go(1).

The setTimeout() call schedules the function to run after the current execution cycle. However, this technique should not be considered a reliable way to permanently disable the browser's Back button.

Can JavaScript Really Disable the Browser Back Button?

No. Modern browsers do not provide a standard JavaScript mechanism that completely disables the user's Back or Forward browser controls. JavaScript can manipulate or respond to session history, but it cannot take away the browser's navigation controls from the user.

This distinction is important when using older snippets that are described as "disable back button" scripts. They may appear to work in particular navigation scenarios, but their behavior depends on the browser's history state and the way the user reached the page.

A Better Approach: Manage History Instead

For modern web applications, the History API provides methods such as pushState(), replaceState(), back(), forward(), and go() for working with browser session history. These APIs are particularly useful when building single-page applications where the URL and displayed content need to remain synchronized with browser navigation.

For example, a page can create a history entry using pushState() and then respond to browser Back and Forward navigation with the popstate event:

history.pushState({ page: "example" }, "", "/example");window.addEventListener("popstate", function (event) {    console.log("Browser history changed:", event.state);});

This approach is generally more appropriate when the goal is to manage application navigation rather than attempting to block the browser's Back button.

Handling Pages Restored with the Back Button

Modern browsers can use a feature called the back/forward cache (bfcache), which can restore a previously visited page from memory instead of loading it again from the network. The pageshow event is useful when an application needs to detect when a document is displayed again, including when it is restored through browser Back or Forward navigation.

window.addEventListener("pageshow", function (event) {    if (event.persisted) {        console.log("Page restored from the back/forward cache.");    }});

When Should You Use This Technique?

Rather than trying to block browser navigation, consider handling the application's state correctly when users navigate backward. This is particularly useful for:

  • Single-page applications
  • Multi-step forms and workflows
  • Checkout and transaction flows
  • Authentication and session-based applications
  • Applications that need to restore previous UI states
  • Pages that need to refresh or validate state after history navigation

What About beforeunload?

If your goal is to warn users about losing unsaved information, beforeunload may be more appropriate than trying to disable Back navigation. It can trigger a browser-controlled confirmation dialog when a user attempts to leave a page, although it has limitations and should be used only when there is a genuine risk of losing unsaved data.

For example:

window.addEventListener("beforeunload", function (event) {    event.preventDefault();    event.returnValue = true;});

Keep in mind that modern browsers control the wording of these dialogs, and beforeunload is not reliably triggered in every situation, particularly on some mobile scenarios.

Final Thoughts

The original history.forward() technique can demonstrate how browser history navigation works, but it should not be presented as a guaranteed method for disabling the Back button. For modern websites, the better solution is to design navigation and application state so that Back and Forward actions behave predictably.

If you are developing a complex web application, understanding the History API, popstate, pageshow, and browser caching behavior can help you create a much more reliable navigation experience.

Need Help With Custom Web Development?

Building reliable navigation is only one part of developing a modern web application. If your project needs custom JavaScript development, web application development, API integration, performance optimization, automation, or ongoing maintenance, the ScriptBaker team can help.

We build and modernize custom web applications with a focus on performance, scalability, usability, and reliable functionality.

Have a web development project in mind? Get in touch with ScriptBaker to discuss your requirements and find the right technical solution for your application.

Explore ScriptBaker Web Development Services →