How to stop user from leaving page after submitting form (i.e. while it's processing)?
<p>There are lots of similar questions on here, but I haven't found any that deal with stopping a user from leaving a page <strong>after</strong> submitting a form (i.e. while it's processing).</p>

<p>I have a page on my website with a form that takes a long time to submit (sometimes as long as 15 seconds), so I want to have a popup warning to keep the user on the page after they submit the form.</p>

<p>The problem is that no matter how I try using <code>window.onbeforeunload</code>, I always get a confirmation dialog from that function when the form is submitted, which is not what I want. I don't want the user to get the confirmation dialog when the form is submitted, but rather after it's submitted and only if they try to leave the page after that.</p>

<p>HTML:</p>

<pre><code>&lt;form id="myform" method="post" action="" onsubmit="formSubmitted();"&gt;
    &lt;button type="submit" onclick="return checkForm();"&gt;submit form&lt;/button&gt;
&lt;/form&gt;
</code></pre>

<p>Javascript:</p>

<pre><code>formSubmittedFlag = 0;

function checkForm()
{
    ...

    if(...)
    {
        return true;
    }
    else
    {
        return false;
    }
}


function formSubmitted()
{
    formSubmittedFlag = 1;
}



window.onbeforeunload = function()
{
    if(formSubmittedFlag == 1)
    {
        return "Are you sure you want to leave the page?";
    }
}
</code></pre>

<p>Is there a way to accomplish what I'm wanting?</p>
