3

I need to call the jquery ajax() function before a window unloads. In my case its a POST and I dont care about the return result, I just want a database to be updated.

So the answer seems to be to bind a function to the unload event, i.e. use the jquery unload() function, and that function makes an ajax call. In addition, I know you are supposed to add the synchronous parameter to the call. In other words, add async: false to the jquery ajax() call.

However, I have tried all this and it does not work on IE8! I have tested it many times and it simply will not work on IE8. Works fine in Firefox and Chrome.

Even stranger is that after I close the whole IE8 application, it seems as if then the call goes through! Is it being cached?

So other people are suggesting to bind the ajax beforeunload using jquery bind(). However there seems to be some mixed answers on whether this will work on all browsers.

I have not yet tried the beforeunload as this seems like the "hack" way of doing it. However, if it works then I dont really care as long as it does work.

Chris Shouts
  • 5,377
  • 2
  • 29
  • 40

1 Answers1

0

Can you not call the function in the body tag of the page?

<body onbeforeunload="funcName(param)">

that way every time the page unloads it calls your function and you don't have to worry about binding.

Loogawa
  • 389
  • 6
  • 24
  • 1
    OK, but thats just equivalent to using jquery bind( "beforeunload" ). Thats the issue, should the ajax call be bound to unload or beforeunload? –  Mar 28 '11 at 18:00
  • Oh I see. I would bind it to beforeunload because binding it to unload causes errors in InternetExplorer and older browsers because it tries to run at the same time as the browsers closing processes. Personally I use beforeunload everytime. – Loogawa Mar 28 '11 at 18:06
  • Sounds reasonable. Do you have a link to where the problems with unload for IE are documented? –  Mar 28 '11 at 18:09
  • http://www.aspxtutorial.com/post/2011/02/06/Difference-between-javascript-OnUnload-VS-OnBeforeUnload-Events.aspx – Loogawa Mar 28 '11 at 18:17
  • I can't find actual documentation saying it doesn't work. And on most microsoft sites I've looked at (like msdn) the mods will say there is no problem even though all of the users are saying there are. But I have found a lot of other websites saying users have had a problem with it in IE – Loogawa Mar 28 '11 at 18:18
  • Thanks for the effort. I will check this stuff out. –  Mar 28 '11 at 19:05