Nik*_*240 5 html javascript jquery onbeforeunload onunload
我需要在用户离开页面之前提示用户,在确认时关闭选项卡,如果没有做任何事情.我想发一个ajax电话onbeforeunload.
我唯一的想法是为两者编写处理程序,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 }
);
};
Run Code Online (Sandbox Code Playgroud)
但这似乎不适用于jsFiddle
它在这个小提琴中工作得很好--> 完美地工作小提琴!!!
function warning(){
if(true){
return "You are leaving the page";
$.ajax({
url: "test.php",
type: "post",
data: values to send
})
}
}
window.onbeforeunload = warning;
Run Code Online (Sandbox Code Playgroud)