问题
I have an aspx page. On the page I have a javascript function
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?";
}
This executes when the user leaves the page. However it also executes on the partial page postbacks. How do I check to see if this is a partial page postback and ignore it?
回答1:
Can I prevent window.onbeforeunload from being called when doing an AJAX call
See if the action you are doing does any of the following:
onbeforeunload event
To invoke this event, do one of the following:
- Close the current window.
- Navigate to another location by entering a new address or selecting a Favorite.
- Click an anchor that refers to another document.
- Invoke the anchor.click method.
- Invoke the document.write method.
- Invoke the document.close method.
- Invoke the window.close method.
- Invoke the window.navigate or NavigateAndFind method.
- Invoke the location.replace method.
- Invoke the location.reload method.
- Specify a new value for the location.href property.
- Submit a form to the address specified in the ACTION attribute via the INPUT type=submit control, or invoke the form.submit method.
- Invoke the window.open method, providing the possible value _self for the window name.
- Invoke the document.open method.
- Click the Back, Forward, Refresh, or Home button.
来源:https://stackoverflow.com/questions/10300250/ajax-and-onbeforeunload-event