使用Javascript window.confirm停止window.event?

时间:2010-06-19 00:32:55

标签: javascript jquery

我正在尝试关闭窗口时出现一个确认框,允许用户停留在当前页面上(通过单击取消),或者继续关闭窗口(通过单击确定)。 / p>

我的代码如下......

<script>     
    function confirm_exit(){
        var message = window.confirm("My message.");
        if (message == true) {
            // Output when OK is clicked
            window.close();
        } else {
            // Output when CANCEL is clicked
            ???????
        }
    }
</script>

我不确定,因为我点击任一个关闭窗口。我需要取消保留在当前页面,然后单击确定关闭窗口或继续用户window.event。

希望这是有道理的。

2 个答案:

答案 0 :(得分:1)

礼貌的certain website with a dash名字:

window.onbeforeunload = leaveConfirm;
function leaveConfirm() {
    var leaveThePageMessage = 'Are you sure you want to leave this page?';
    return leaveThePageMessage;
}​

这适用于所有浏览器(Opera除外)。

答案 1 :(得分:1)

尝试:

window.onbeforeunload = function()
{
  return confirm('Are you sure you want to close?');
}