I am doing an online quiz application in PHP. I want to restrict the user from going back in an exam.
I have tried the following script, but it stops my timer.
Very simple and clean function to break the back arrow without interfering with the page afterward.
Benefits:
onbeforeunload
setInterval
so it doesn't break slow browsers and always works.unbeforeunload
which interrupts user with modal dialog.Note: some of the other solutions use onbeforeunload
. Please do not use onbeforeunload
for this purpose, which pops up a dialog whenever users try to close the window, hit backarrow, etc. Modals like onbeforeunload
are usually only appropriate in rare circumstances, such as when they've actually made changes on screen and haven't saved them, not for this purpose.
How It Works
That's it. No further messing around, no background event monitoring, nothing else.
Use It In One Second
To deploy, just add this anywhere on your page or in your JS: