当用户关闭窗口时 Onbeforeunload 不会在 IE 中触发

发布于 2024-11-19 19:02:20 字数 813 浏览 8 评论 0原文

我通过绑定到所有输入字段的“更改”事件来监视表单中每个字段的更改,并在有任何修改时设置一个标志。
然后,我绑定到 window.onbeforeunload 并检查该标志,如果设置了该标志,则返回警告。根据 Jquery API 文档,我直接绑定到 window.onbeforeunload 与 $(window) 以避免内存问题。

为此,我有以下代码:

$(':input').change(function(){
    if($('#editObject_blah').attr('value') == 0){
        $('#editObject_blah').attr('value',1)};
    }
);
window.onbeforeunload=verifyexit;
function verifyexit() {
    if($('#editObject_blah').attr('value') == 1){
        return'You have not saved your changes!';
    }
};

编辑:元素 editObject_blah 实际上是这样的:

<form id="editObject_blah" onSubmit="return false;" value=0>

这在 Firefox 和 Chrome 中工作正常,但无法捕获用户在 IE 7 中关闭浏览器窗口。

我应该注意,上面的代码是通过eval() 作为 ajax 请求的结果,而不是通过内联脚本标记加载。我认为这不会有什么不同,但这就是我在这里问的原因。

谢谢。

I monitor each field in my form for changes by binding to the 'change' event for all input fields, and set a flag if anything gets modified.
I then bind to window.onbeforeunload and check that flag, returning the warning if the flag is set. Per the Jquery API documentation, I'm binding directly to window.onbeforeunload versus $(window) to avoid memory issues.

For this I have the following code:

$(':input').change(function(){
    if($('#editObject_blah').attr('value') == 0){
        $('#editObject_blah').attr('value',1)};
    }
);
window.onbeforeunload=verifyexit;
function verifyexit() {
    if($('#editObject_blah').attr('value') == 1){
        return'You have not saved your changes!';
    }
};

EDIT: The element editObject_blah is actually this:

<form id="editObject_blah" onSubmit="return false;" value=0>

This works fine in Firefox and Chrome, but fails to catch the user closing the browser window in IE 7.

I should note that the above code is called via an eval() as the result of an ajax request, versus being loaded via an inline script tag. I don't think that should make a difference, but that's why I'm asking here.

Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

吾家有女初长成 2024-11-26 19:02:20

您不应该使用“.attr()”来获取“value”属性:

if ($('#editObject_blah').val() == 1) {
  return "whatever";
}

如果您的处理程序不返回字符串,则浏览器会假设您同意用户关闭窗口。

如果这不是 ,那么您可能应该使用“.data()”来存储该标志。为此,您只需为标志选择一个名称:

$(':input').change(function(){
  $('#editObject_blah').data("changed", 1);
});

function verifyexit() {
  if($('#editObject_blah').data('changed') == 1){
    return "You have not saved changes yet!";
  }
}

You shouldn't use ".attr()" to get the "value" property:

if ($('#editObject_blah').val() == 1) {
  return "whatever";
}

If your handler does not return a string, then the browser assumes you're OK with the user closing the window.

If that's not an <input>, then you should probably store the flag with ".data()" instead. To do that, you'd just pick a name for the flag:

$(':input').change(function(){
  $('#editObject_blah').data("changed", 1);
});

function verifyexit() {
  if($('#editObject_blah').data('changed') == 1){
    return "You have not saved changes yet!";
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文