Click here to Skip to main content
15,887,329 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I have several functions running on a postback that can take a little time to complete.

When postback is initiated I show a loading image with this code:

XML
function showLoader()
{
    document.getElementById("<%=loadingImage.ClientID%>").style.visibility="visible";
}


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.

I found this code:

C#
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;


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

I tried the following but it shows the alert message when the page eventualy posts back:

C#
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("<%=loadingImage.ClientID%>").style.visibility="visible";
    window.onbeforeunload=goodbye;
}



Any ideas how I can tweak this to just show when user leaves page and not when postback completes?
Posted
Updated 13-Mar-11 11:38am
v2

1 solution

You know you've got a bigger problem than the user leaving. Your operation has to be independant of the users state. Why? You cannot prevent the user from closing the browser!
 
Share this answer
 
Comments
Guerrilla123 13-Mar-11 19:12pm    
Not sure I understand you fully. If they close the browser then fair enough. I have observed through some analytic software that I installed that I am missing submissions by people clicking submit then clicking another link on my site before the page has posted back.

I just want to inform them "Hey, form is still submitting, move away now and you wont recieve goodies to your email".

Perhaps I need to nest the goodbye() function inside an if statement but I just can't see how to test this condition.

I think I might need to do something with events/event args, but I don't know exactly how.
Dave Kreskowiak 13-Mar-11 22:31pm    
You cannot depend on the user waiting for a completion. No amount of code you supply will prevent the user from leaving the page before the postback operation is done. HTTP is stateless and your business layer has to deal with that. There are a variety of ways of doing that, but trying to prevent the user from navigating to another page is not one of them. Trying to rely on the user staying in one place waiting while your code does it's work is not reliable. What is the users browser just hangs are this point? Your client-side script cannot do anything about it...

Your business logic has to be able to handle a possibly non-existant, stateless client.
Albin Abel 14-Mar-11 0:11am    
Yes Http is stateless and compelling customers is not right way. Instead 1) Request the customer wait if they want 2) If you can predict preload the busy process result. There are lot of other ways too. My 5
Guerrilla123 14-Mar-11 8:02am    
If they navigate away it doesn't cause me any problems in my code, it just means I may not get a sales lead.

I sometimes require user confirmation after the postback otherwise I could write details to a database and have a seperate process carry out the labourous work.

Basically all I want is to get a javascript exit script working that doesn't trigger during postback.

Is "window.onbeforeunload" meant to trigger during postback or is it just the way I have called it?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900