0 0 0 0


I am doing online quiz app in php. I want to restrict user while going back in exam. I have tried following script but it stops my timer. What should I do please suggest me. I have included source code. The timer is stored in cdtimer.js

<script type="text/javascript">
        function noBack()
<body onLoad="noBack();" onpageshow="if (event.persisted) noBack();" onUnload="">

I have exam timer which takes duration of exam from mysql db and starts timer accordingly it stops when I put code for disabling back button. what will be the problem.

Best Answer:

It is generally a bad idea overriding the default behavior of web browser. Client side script does not have the sufficient privilege to do this for security reason.

There are few similar questions asked as well,

You can-not actually disable browser back button. However you can do magic using your logic to prevent user from navigating back which will create an impression like it is disabled. Here is how, check out the following snippet.

(function (global) { 
    if(typeof (global) === "undefined") {
        throw new Error("window is undefined");
    var _hash = "!";
    var noBackPlease = function () {
        global.location.href += "#";
        // making sure we have the fruit available for juice (^__^)
        global.setTimeout(function () {
            global.location.href += "!";
        }, 50);
    global.onhashchange = function () {
        if (global.location.hash !== _hash) {
            global.location.hash = _hash;
    global.onload = function () {            
        // disables backspace on page except on input fields and textarea..
        document.body.onkeydown = function (e) {
            var elm =;
            if (e.which === 8 && (elm !== 'input' && elm  !== 'textarea')) {
            // stopping event bubbling up the DOM tree..

This is in pure JavaScript so it would work in most of the browsers. It would also disable backspace key but key will work normally inside input fields and textarea.

Recommended Setup:

Place this snippet in a separate script and include it on a page where you want this behavior. In current setup it will execute onload event of DOM which is the ideal entry point for this code.

Working DEMO!

Tested and verified in following browsers,

  • Chrome.
  • Firefox.
  • IE (8-11) and Edge.
  • Safari.

Copyright © 2011 Dowemo All rights reserved.    Creative Commons   AboutUs