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">
window.history.forward();
function noBack()
{
window.history.forward();
}
</script>
<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.
Try this to prevent backspace button in IE which by default act as "Back":
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,
How can I prevent the backspace key from navigating back?
How to prevent browser's default history back action for backspace button with JavaScript?
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.
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 andtextarea
.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,
This javascript does not allow any user to go back (works in Chrome, FF, IE, Edge)