javascript - AJAX 和 onbeforeunload 事件

标签 javascript asp.net ajax updatepanel

我有一个 aspx 页面。在页面上我有一个 javascript 函数

window.onbeforeunload = confirmExit;
function confirmExit()
{
return "You have attempted to leave this page.  If you have made any changes to the fields      without clicking the Submit button, your changes will be lost.  Are you sure you want to exit   this page?";

}   

当用户离开页面时执行。但是它也会在部分页面回发上执行。如何检查这是否是部分页面回发并忽略它?

最佳答案

Can I prevent window.onbeforeunload from being called when doing an AJAX call

查看您正在执行的操作是否执行以下操作:

onbeforeunload event

要调用此事件,请执行以下操作之一:

  • 关闭当前窗口。
  • 通过输入新地址或选择收藏夹导航到另一个位置。
  • 单击引用另一个文档的 anchor 。
  • 调用anchor.click方法。
  • 调用 document.write 方法。
  • 调用 document.close 方法。
  • 调用window.close方法。
  • 调用 window.navigate 或 NavigateAndFind 方法。
  • 调用 location.replace 方法。
  • 调用 location.reload 方法。
  • 为 location.href 属性指定新值。
  • 将表格提交至中指定的地址 通过 INPUT type=submit 控件的 ACTION 属性,或调用 form.submit 方法。
  • 调用 window.open 方法,提供 窗口名称的可能值 _self。
  • 调用 document.open 方法。
  • 点击“后退”、“前进”、“刷新”或“主页”按钮。

关于javascript - AJAX 和 onbeforeunload 事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10300250/

相关文章:

javascript - 正则表达式与我的示例不匹配

asp.net - Visual Studio 2010 和 2012 ASP.Net 调试和布局问题

asp.net - 使用用户控件时,元素<element>在Visual Studio中不是已知的元素警告

javascript - 在 asp.net mvc 或 jquery 中刷新时保留类更改

php - Symfony:使用ajax在表格上搜索字段

php - 防止通过 AJAX 调用的 MySQL 存储过程出现 "Commands Out of Sync"错误

javascript - 在请求中设置自定义 header

java - 在 java servlet 中包含 javascript 的最佳方式

Javascript 改变 div 的颜色,然后改变 div 焦点的颜色

jquery - jQuery ajax 调用后 IE 7 重定向