views:

61

answers:

3

On browser close or F5, I have to perform some code on server side For this I have a button. On click of that button which has onclientclick and onclick functions written. I also wrote an event on window.onbeforeunload which does a button.click().

window.onbeforeunload=function(e)
{
   button.click()
}

My problem is that this runs the code of the client side click function of the button, however server side code does not get executed. This happens only when i close the browser. When I do F5 it works perfectly. Also this happens only on Safari. In FF and mozila it works perfectly . How can i

A: 

There is probably a speed issue, the button gets clicked but the response isn't being sent to the server fast enough, so the browser closes before it can be sent.

If you need to, you can always throw up a prompt to slow the closing of the window down, but this is very unfriendly.

Aaron Harun
A: 

Safari does not allow or execute asynchronous AJAX requests in beforeunload events, so it might very well be that your code is executed but that the requests are never issued to the server. Synchronous AJAX requests are executed, so use those in the beforeunload handler.

Gus
A: 

Thanks allot for the tip about synchr. AJAX requests on browser close ;)

It did solved my problem!

Kozie