你能在JavaScript中触发事件在用户关闭窗口前? [英] Can you fire an event in JavaScript before the user closes the window?
本文介绍了你能在JavaScript中触发事件在用户关闭窗口前?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我要发送一个ping到使用AJAX GET或POST,之后他们关闭该窗口我的服务器。
为 window.onbeforeunload
是个好主意?
I want to send a ping to my server using an AJAX GET or POST, right after they close the window.
Is window.onbeforeunload
a good idea?
推荐答案
是的,你可以把在 window.onbeforeunload
,但它不能保证你一个AJAX请求会得到响应的浏览器关闭,所以你只能发请求,但不依赖读取响应之前。如果你需要得到回应,你可以发送异步的同步请求代替。
Yes, you can send an AJAX request in the window.onbeforeunload
but it is not guaranteed that you will get a response before the browser closes so you could only send the request but don't rely on reading a response. If you need to get a response you could send a synchronous request instead of asynchronous.
这篇关于你能在JavaScript中触发事件在用户关闭窗口前?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文