2012-04-24 46 views
1

我有一个aspx页面。在页面上我有一个javascript函数AJAX和onbeforeunload事件

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?"; 

} 

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

回答

0

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

见,如果你正在做的动作做以下任何一项:

onbeforeunload event

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

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

让我举一个例子。在aspx表单上我有一个控件。当我从这个控件中选择一个选项时,会有一个部分回传,并执行confirmExit()函数。我如何知道这是一个下拉选择,而不是实际的导航离开页面? – user1247395 2012-04-24 18:17:05