On browser close or F5, I have to 开发者_C百科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
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.
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.
as a curiosity, The problem here is taht Safari does not recognize the onbeforeunload event... therefore any code there will not be run in Safari. You could use onunload event but here you will never be able to prevent the page from closing
精彩评论