Reputation: 2554
well guys before i tried to write this post, please be assured that i tried onbeforeunload event in all chrome / firefox and IE, it is working fine in both IE and Firefox but failing in chrome.
I however in one instance noticed that i am able to make an ajax call async set to false, but that didnt work fine for a cross site ajax call.
to give a background on what i am trying to do ... i am trying to build a record and playback js engine for automation testing using jquery and i am using $.ajax for callbacks.
you can find the demonstration here
http://orgchartasp.net/jtest/ff.swf.html
what i am doing is, capturing all the events on the client side and posting back to server onbeforeunload event, i finally succeeded with IE and FF by opening up a modal window on before unload and close it after save.
have anyone really succeeded with onbeforeunload cross browser / cross site , if yes please help me out.
Upvotes: 1
Views: 1189
Reputation: 700302
The thing with the onbeforeunload
event is that it's too late to post anything back to the server in the current window. The action that caused the event, i.e. that the browser is about to load a new page, has already decided what page to load next.
The exact behaviour of the browser when handling this event may vary, as it wasn't even in any standard until HTML5.
If you want to send anything to the server from the event, you have to open a new window and post from that, or make an AJAX call. It's a fire-and-forget though, as you can't wait for any response.
I would say that you should not rely on the onbeforeunload
to work reliably, so you shouldn't build your architecture around it.
Upvotes: 1