2012-04-24 128 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