Stop User Leaving Before Postback (Javascript/ASP.NET)
<p>I have several functions running on a postback that can take a little time to complete.</p>

<p>When postback is initiated I show a loading image with this code:</p>

<pre><code>function showLoader()
{
    document.getElementById("&lt;%=loadingImage.ClientID%&gt;").style.visibility="visible";
}
</code></pre>

<p>I want to be able to add code to this function so if user tries to leave at this point they are informed the operation is not complete.</p>

<p>I found this code:</p>

<pre><code>function goodbye(e) {
    if(!e) e = window.event;
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
    //e.stopPropagation works in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
}
window.onbeforeunload=goodbye;
</code></pre>

<p>This works but I only want the code to be active when the loading image is active.</p>

<p>I tried the following but it shows the alert message when the page eventually posts back:</p>

<pre><code>function goodbye(e) {
    if(!e) e = window.event;
    //e.cancelBubble is supported by IE - this will kill the bubbling process.
    e.cancelBubble = true;
    e.returnValue = 'You sure you want to leave?'; //This is displayed on the dialog
    //e.stopPropagation works in Firefox.
    if (e.stopPropagation) {
        e.stopPropagation();
        e.preventDefault();
    }
}
function showLoader()
{
    document.getElementById("&lt;%=loadingImage.ClientID%&gt;").style.visibility="visible";
    window.onbeforeunload=goodbye;
}
</code></pre>

<p>Any ideas how I can tweak this to just show when user leaves page and not when postback completes? </p>
