5

我需要在用户离开页面之前提示用户,确认关闭选项卡,如果没有,则不执行任何操作。我想发送一个 ajax 调用onbeforeunload

我唯一的想法是为两者编写处理程序onunloadonbeforeunload如下所示:

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 中不起作用

4

2 回答 2

1

它在这个小提琴中工作正常--> 完美工作的小提琴!!!

 function warning(){
            if(true){
                return "You are leaving the page";
                 $.ajax({
                        url: "test.php",
                        type: "post",
                        data: values to send
                        })
            }
        }
        window.onbeforeunload = warning;
于 2013-07-26T19:39:57.693 回答
0

把它们都放在里面onbeforeunload

window.onbeforeunload = function(){
    $.get(
        "http://www.mysite.com/php/myhandler.php",
        { data: 1 }
    );
    return 'Are you sure you want to leave?';
};
于 2013-07-26T19:35:57.907 回答