Tuesday, March 4, 2008

onBeforeUnLoad

onBeforeUnLoad can go to hell and die.... words can not express how my BRC is rising (Blood Rage-ohol Content). So it turns out, that it is not worth using onBeforeUnLoad instead of onUnLoad. One of my teachers referred me to that for the 'IE crowd', this has caused so many screw up its not even funny. The things in the function that is called from this event will work erratically at best, unless you have an alert in there. I realize that this is most likely a timing issue or even compatibility. So what happened was I would call this function from onBeforeUnload, and it would or would not my one line of AJAX code (jQuery). This caused me to put in an alert to test it out, the alert would always fire, and my AJAX would successfully execute. Take out alert, call fails. Now I have said 'forget about that noise just use onUnLoad' and everything works peachy, including in IE. So the moral of this story... use onUnLoad unless you are POSITIVE people will ONLY be using IE

2 Comments:

Anonymous Anonymous said...

The AJAX event is likely firing, as it should, but the script continues to run and the user leaves the page before the AJAX call is complete. You should use something to delay the page from continuing until the AJAX request is complete. Something like:

window.onbeforeunload = function(){
if(updateUserInfo()){
return true;
}
}

function updateUserInfo(){
//all your ajax code
}

April 6, 2009 at 6:13 PM  
Anonymous Jennifer said...

That won't help because it would still be an asynchronous call.

You could use the synchronous version of XMLHttpRequest instead, but you'd run the risk of hanging the user's browser if the server request takes too long.

June 2, 2009 at 5:32 PM  

Post a Comment

Subscribe to Post Comments [Atom]

<< Home