在这里的stackoverflow,如果你开始做改变,然后你试图导航离开页面,一个javascript确认按钮显示,并询问:“你确定你想导航离开这个页面吗?”

以前有人实现过这个吗?我如何跟踪已提交的更改? 我相信我自己可以做到这一点,我正在努力从你们这些专家那里学习好的做法。

我尝试了以下方法,但仍然不起作用:

<html>
<body>
    <p>Close the page to trigger the onunload event.</p>
    <script type="text/javascript">
        var changes = false;        
        window.onbeforeunload = function() {
            if (changes)
            {
                var message = "Are you sure you want to navigate away from this page?\n\nYou have started writing or editing a post.\n\nPress OK to continue or Cancel to stay on the current page.";
                if (confirm(message)) return true;
                else return false;
            }
        }
    </script>

    <input type='text' onchange='changes=true;'> </input>
</body>
</html>

有人能举个例子吗?


当前回答

onbeforeunload Microsoft-ism是我们拥有的最接近标准解决方案的东西,但请注意浏览器的支持是不均衡的;例如,对于Opera,它只适用于版本12及更高的版本(在撰写本文时仍处于测试阶段)。

此外,为了最大限度地兼容,您需要做的不仅仅是返回一个字符串,正如Mozilla开发者网络中所解释的那样。

示例:定义以下两个函数用于启用/禁用导航提示符(参见MDN示例):

function enableBeforeUnload() {
    window.onbeforeunload = function (e) {
        return "Discard changes?";
    };
}
function disableBeforeUnload() {
    window.onbeforeunload = null;
}

然后像这样定义一个表单:

<form method="POST" action="" onsubmit="disableBeforeUnload();">
    <textarea name="text"
              onchange="enableBeforeUnload();"
              onkeyup="enableBeforeUnload();">
    </textarea>
    <button type="submit">Save</button>
</form>

这样,只有当用户更改了文本区域时,才会提示用户导航离开,而当用户实际提交表单时不会收到提示。

其他回答

更新(2017)

现代浏览器现在认为显示自定义消息是一种安全隐患,因此它已从所有浏览器中删除。浏览器现在只显示通用消息。因为我们不再需要担心设置消息,它就像这样简单:

// Enable navigation prompt
window.onbeforeunload = function() {
    return true;
};
// Remove navigation prompt
window.onbeforeunload = null;

阅读下面的内容,了解传统浏览器的支持。

更新(2013)

最初的答案适用于IE6-8和FX1-3.5(这是我们在2009年写它时的目标),但现在已经过时了,不能在大多数当前的浏览器中工作-我把它留在下面作为参考。

窗外。并非所有浏览器都一致对待Onbeforeunload。它应该是一个函数引用,而不是一个字符串(如最初的答案所述),但在旧的浏览器中也可以工作,因为对大多数浏览器的检查似乎是是否有任何东西被分配给onbeforeunload(包括返回null的函数)。

你设置窗口。onbeforeunload到一个函数引用,但在旧的浏览器中,你必须设置事件的returnValue,而不仅仅是返回一个字符串:

var confirmOnPageExit = function (e) 
{
    // If we haven't been passed the event get the window.event
    e = e || window.event;

    var message = 'Any text will block the navigation and display a prompt';

    // For IE6-8 and Firefox prior to version 4
    if (e) 
    {
        e.returnValue = message;
    }

    // For Chrome, Safari, IE8+ and Opera 12+
    return message;
};

如果你想让用户在没有消息的情况下继续,你不能让confirmOnPageExit执行检查并返回null。你仍然需要删除事件来可靠地打开和关闭它:

// Turn it on - assign the function that returns the string
window.onbeforeunload = confirmOnPageExit;

// Turn it off - remove the function entirely
window.onbeforeunload = null;

原答案(2009年工作)

打开它:

window.onbeforeunload = "Are you sure you want to leave?";

关闭:

window.onbeforeunload = null;

请记住,这不是一个正常的事件—您不能以标准方式绑定到它。

检查值?这取决于您的验证框架。

在jQuery中,这可能是(非常基本的例子):

$('input').change(function() {
    if( $(this).val() != "" )
        window.onbeforeunload = "Are you sure you want to leave?";
});

jquery 'beforeunload'对我来说非常有用

$(window).bind('beforeunload', function(){
    if( $('input').val() !== '' ){
        return "It looks like you have input you haven't submitted."
    }
});

这可以很容易地通过设置一个ChangeFlag为true,在TextArea的onChange事件。使用javascript根据ChangeFlag值显示确认对话框。如果确认返回true,则丢弃表单并导航到请求页面,否则什么都不做。

body标签有一个"onunload"参数,你可以从那里调用javascript函数。如果返回false,则阻止导航离开。

当用户开始对表单进行更改时,将设置一个布尔标志。如果用户尝试离开页面,则检查窗口中的标志。onunload事件。如果设置了标志,则通过以字符串形式返回消息来显示消息。以字符串形式返回消息将弹出一个包含您的消息的确认对话框。

如果你使用ajax来提交更改,你可以在更改提交后(即在ajax成功事件中)将标志设置为false。