可能重复:
How to execute ajax function onbeforeunload?
How to capture browser close event and make a request to web service method on that event through javascriptJavaScript的onbeforeunload火get请求
所以现在这是我onbeforeunload的样子
window.onbeforeunload = function (e) {
var message = "Don't close me!",
e = e || window.event;
// For IE and Firefox
if (e) {
e.returnValue = message;
}
// For Safari
return message;
};
因此,这是什么我正在尝试这个函数会触发并提供一个提示来选择是否离开页面。当且仅当他们决定离开时,我需要发起GET请求来关闭数据库记录。
从测试,如果我设置这样的:
window.onbeforeunload = $.get("url_here")
这是一个简单的办法但现在我不能得到及时和窗口无预警关闭。
我也尝试添加一个确认功能,但它似乎并没有正常工作。例如。
window.onbeforeunload = function(e){
if(confirm("Are you sure?")){
$.get("url_here");
return true;
}
}
这一个得到怪异,但是在大多数情况下,它只是导致邮件与文字文本(“真”)的是/否提示弹出。
你不能可靠地做到这一点。 – SLaks
+1 SLaks。可能会有答案指出如何去做,他们只是不总是工作。您必须做其他事情,例如在用户更改数据时保存数据。 –
异步假工程,只要请求使它在服务器的第一秒左右 – Esailija