0

I am working on online examination system using php and mysql and I am new to this programing language. I want to prevent the one taking the exam from leaving the questions page by clicking on the back button or accidentally closing the tab. I want to warn the user that all his work will be lost if he leaves the page and if he decides to leave then he/she will get zero marks on the exam.

I have found many people providing solutions to this problem but none of them worked with me, so,it would be nice if any one can help with a simple code that can do what I need using javascript

Jon
  • 9,156
  • 9
  • 56
  • 73
aaa ggg
  • 7
  • 2
  • Sounds like you're looking for the `onbeforeunload` event: https://developer.mozilla.org/en-US/docs/Web/API/WindowEventHandlers/onbeforeunload – David Sep 13 '17 at 17:18
  • Possible duplicate of [Intercept page exit event](https://stackoverflow.com/questions/1704533/intercept-page-exit-event) – Andrew Myers Sep 13 '17 at 17:57
  • If solutions to duplicate questions don't work for you, please tell us which solutions you tried and **why** they didn't work for you (what happened that should have happened, and any error message you received). – Andrew Myers Sep 13 '17 at 17:59
  • @AndrewMyers I tried what you provided,but the back button doesn't work once the page is loaded directly. Actually the warning can appear after I click a link on my page ,only after that the back button starts to function, but if i click the back button directly after loading the page it takes me to the previous page without warning or alert box. Any ideas?? – aaa ggg Sep 13 '17 at 18:33

0 Answers0