-2

I am trying to develop a webpage where the user is giving online test and if he has not submitted that test and then try to navigate to another page, he should get a confirmation message that his answers will not be saved if he leaves his test. How shall i go?

  • Possible duplicate of [How to show the "Are you sure you want to navigate away from this page?" when changes committed?](http://stackoverflow.com/questions/1119289/how-to-show-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-ch) – jeteon Mar 04 '16 at 14:58

2 Answers2

1

You can use onbeforeunload event:

var submitted = false;
window.onbeforeunload = function() {
   if (!submitted) {
       return "Are you sure you want to leave this page, your test will not be saved?";
   }
};
document.getElementById('someForm').onsubmit = function() {
    submitted = true;
    // submit the form using ajax
};
jcubic
  • 61,973
  • 54
  • 229
  • 402
0

Create a function which validates the form. When the user click on 'a' tag (trying to navigate), call that function and alert him if he not submitted the test.

Ramesh
  • 32
  • 1
  • 7
  • i am doing this actually-this is my script this is where i am calling that script-
    it works fine on mouseleave but i want it to work only when the user has not submitted
    – Shara Rizvi Mar 04 '16 at 15:09
  • @Ramesh, can you post some code to demonstrate what you mean? Thanks – Suever Mar 05 '16 at 14:21