How to completly disable back button of a browser while taking online exam?

How to completly disable back button of a browser while taking online exam?

  1. Blog
  2. 15 November 2026
  3. 3 min read

Stefan Bogdanescu

Founder & Senior Architect

The Illusion of Browser Back Button Disabling in Online Exams

When building an online examination system, security and flow integrity are paramount. A common requirement is to prevent candidates from navigating away from the exam interface once they begin. Developers often attempt to solve this by manipulating browser history or clearing the cache, hoping that a "Document Expired" message will deter users. However, relying on cache expiration is not only poor user experience but also technically fragile; it does not actually disable the back button's functionality, nor does it prevent sophisticated users from bypassing these checks.

To truly restrict navigation while maintaining a seamless flow until submission, you need to intercept the browser's history management logic directly within your application code. The most robust approach involves utilizing the HTML5 History API to control how the browser stack behaves.

The standard pattern for this scenario is to replace the current page in the session history with a blank state or a specific "locked" state before rendering the exam content. When the user clicks the back button, the browser attempts to revert to the previous entry in its session history. If that entry is an empty document or a custom error state rather than the actual login or dashboard page, the navigation appears blocked.

Here is how you can implement this logic using standard JavaScript:

// Replace current history entry with an empty document
history.replaceState({}, document.title, window.location.href);

// Listen for back/forward events to handle them gracefully
window.addEventListener('popstate', function(event) {
    // Prevent default behavior if you want the page to stay exactly as is
    event.preventDefault(); 
    
    // Optionally show a toast notification or log the attempt
    console.log('Navigation attempt detected.');
    
    // Note: You cannot programmatically redirect back to the previous URL 
    // without breaking the flow, so preventing default keeps them on the exam.
});

// Initialize with an empty document state immediately
document.body.innerHTML = '';

By calling history.replaceState, you overwrite the current entry in the session history without adding a new one. When the user clicks back, the browser loads the state stored at that index. Since you have replaced it with an empty document (or a custom placeholder), there is no previous page to return to in the context of the exam session.

It is crucial to understand the distinction between history.back() and cache clearing. Clearing the cache forces the browser to reload resources, which might trigger a 404 or expiration error if the server does not have the resource cached anymore. However, this relies on server configuration (like short TTLs for HTML) and is easily circumvented by disabling cache in developer tools. The History API method works regardless of cache settings because it manipulates the navigation stack itself, not the storage layer.

In a Laravel application, you would typically trigger this logic upon clicking the "Start Exam" button. You can attach a listener to the form submission event to execute the JavaScript that locks the history stack before the page fully renders or reloads if necessary.

document.getElementById('start-exam-btn').addEventListener('click', function() {
    // Lock the history immediately upon start click
    window.history.replaceState({}, document.title, window.location.pathname);
    
    // Redirect to exam view or show modal depending on your architecture
    // Ensure no new state is pushed to history during the transition
});

One significant trade-off here is that this technique prevents returning to the specific exam page via the back button, but it does not prevent users from using bookmarks, direct URL entry, or opening the site in a new tab. For high-stakes environments, client-side restrictions should always be paired with server-side validation. You must verify on every request that the user is still within the active exam session and that they haven't left via an external link.

If you need to handle edge cases like browsers that ignore preventDefault() on history navigation, you may need to detect the browser's behavior and provide a fallback message. However, modern browsers respect the stack manipulation logic effectively when implemented correctly. This approach ensures that the candidate remains in the same logical context until they explicitly submit their answers.

Our network