当用户关闭窗口时 Onbeforeunload 不会在 IE 中触发
我通过绑定到所有输入字段的“更改”事件来监视表单中每个字段的更改,并在有任何修改时设置一个标志。
然后,我绑定到 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您不应该使用“.attr()”来获取“value”属性:
如果您的处理程序不返回字符串,则浏览器会假设您同意用户关闭窗口。
如果这不是
,那么您可能应该使用“.data()”来存储该标志。为此,您只需为标志选择一个名称:
You shouldn't use ".attr()" to get the "value" property:
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: