Home » Php » javascript – How to completly disable back button of a browser while taking online exam?

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

Posted by: admin July 12, 2020 Leave a comment


In online examination once candidate has clicks on start button i dont want to allow him to go back (to previous pages) and he should be in same page if he do any activities by using browser properties.Once he click on the submit button he is coming out of that page.

Here if i clear the cache and press the browser back button the page says as “Document Expired The requested document is not available in Firefox’s cache.” Therefore i dont want to clear cach insted i need to competly disable back button ie nothing should happen on clicking the back button.

if anybody knows let me know how to achieve this concept……

How to&Answers:

Try this script

window.location.hash="Again-No-back-button";//again because google chrome don't insert first hash into history

Also refer this link. This link contain demo & Download code


While you can prevent the back button, you can’t prevent the user from disabling JS while he takes the exam. This makes JS approaches practically useless.

Assuming that previous pages are questionaires, I suggest that once the user moves away from the page, any further access to that page will be invalid. This can easily be done using a server-side language and a database to track pages visited by the user.

Do note that some browsers do a “refresh” on the previous page when doing a back, while others load them from the cache. The latter will make the page still accessible even if you marked it inaccessible on the server. What you can further do is to invalidate any action taken on that page once it has been deemed invalid for access. That way, even if the user had a copy of that page, he can’t do anything with it.

Besides, it’s an exam, and JS is a bad place to put some exam logic as it can easily be inspected and modified. You should leave everything on the server instead.


Try with the below code. This code tested with latest Chrome and Firefox browsers.

<script type="text/javascript">
    history.pushState(null, null, location.href);
    window.onpopstate = function () { history.go(1); };


You can use replace()

window.location.replace('your url here');


Try in your page following function to prevent unloading of form.

$(window).on('beforeunload', function(){
      return false;

Or try unload function and prevent unloading of current page silently.


you can not actually override the default behaviour of browsers back button, it is a security concern among browsers.

however you can code in some sense that make your back appear disable. Check one of my responses I gave some time back. how to stop browser back button using javascript. It is still working fine for me (Bad luck for IE-7). But what if some one has disable javascript in browser. This will fail this logic completely which could be a rare case.

another practice you can follow is to open your page link inside window.open(). But it has its own advantages and disadvantages. See Here