我需要在用户离开页面之前提示用户,在确认时关闭选项卡,如果没有做任何事情。我想在onbeforeunload
上发送一个ajax调用。
我唯一的想法是为这样的onunload
和onbeforeunload
编写处理程序:
window.onbeforeunload = function(){
return 'Are you sure you want to leave?';
};
window.onunload = function(){
$.get(
"http://www.mysite.com/php/myhandler.php",
{ data: 1 }
);
};
但这似乎不适用于jsFiddle
答案 0 :(得分:1)
它在这个小提琴中工作得很好 - > Perfectly working fiddle!!!
function warning(){
if(true){
return "You are leaving the page";
$.ajax({
url: "test.php",
type: "post",
data: values to send
})
}
}
window.onbeforeunload = warning;
答案 1 :(得分:0)
只需将所有内容放在onbeforeunload
window.onbeforeunload = function(){
$.get(
"http://www.mysite.com/php/myhandler.php",
{ data: 1 }
);
return 'Are you sure you want to leave?';
};