开发者

Ajax during javascript window.beforeunload: returning error

开发者 https://www.devze.com 2023-04-06 19:37 出处:网络
I need to perform an ajax request to send some data server-side every time the user decides to leave the page. However I do not wish to disrupt them in leaving: I 开发者_如何转开发do not want popup me

I need to perform an ajax request to send some data server-side every time the user decides to leave the page. However I do not wish to disrupt them in leaving: I 开发者_如何转开发do not want popup messages to show up. Currently my code is thus:

window.onbeforeunload=function(){
   unloadAjax();
}

This does not work and causes a momentary ajax-error message to show up. However, if I add a return false; statement inside the onbeforeunload function, the ajax works fine. How do I fix this problem?


When you add return false, a confirmation dialog shows up, delaying the page unload. Meanwhile, the AJAX request finishes. When no return statement is included, the page will immediately unloads, terminating all active connections (and breaking your AJAX request).

0

精彩评论

暂无评论...
验证码 换一张
取 消