0

I am designing a page for quiz contest. I need to submit the quiz when users tries to refresh page. I am using JavaScript. Plz help me..!!

function reload() {
            if(localStorage.load == 1) {
                localStorage.load = +0;
                document.getElementById('quesform').submit();
            }
            set();
        }
        function set() {
            if(!localStorage.load || localStorage.load == 0)
                localStorage.load = 1;
        }

I used this code, but it didn't works in chrome. It executes the coding after submitting the form. It sets value to 1 and redirect immediately before displaying question page..

Mdumanoj
  • 517
  • 3
  • 9
  • 27
  • 1
    Please update your question with your research, what you have tried, other wise you may get serious downvotes – aravind Mar 22 '14 at 08:23
  • 1
    what u have tried so far ? – Anant Dabhi Mar 22 '14 at 08:23
  • Show your progress you have done and ask for help when you have a problem at some point where you are stuck. People wont help you if you have done nothing and just ask. So kindly show your code progress and indicate where you need help – Aatish Sai Mar 22 '14 at 08:23
  • A simple search would lead to this- http://stackoverflow.com/questions/8013429/jquery-detect-page-refresh – Ashutosh Mar 22 '14 at 08:53

1 Answers1

0

I have removed my previous answer because it is not possible to submit a form when the user tries to leave the page.

You are limited to giving the user the choice of leaving/staying.

The only solution would be to set a cookie containing the form data, so that when the user next visits your page, the data can be retrieved and submitted.

Laurent
  • 1,292
  • 4
  • 23
  • 47
  • Thanks for your answer. But I am designing page for quiz. So, If user tries to refresh the page I need to redirected it to the result page and display the current score. Its a one time quiz. – Mdumanoj Mar 23 '14 at 02:31
  • You cannot redirect a user trying to leave the page. You can however detect if their IP address has already visited the page, or set a cookie containing the quiz data when they leave you page. – Laurent Mar 23 '14 at 15:33