简体   繁体   中英

Submit form when page loads using Javascript

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..

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM