问题
My web page contains grid-view with many input fields.I want to give alert to user before moving to next tab. My script is working fine in Mozilla Firefox and chrome but in IE the alert is also coming for grid-view pagination click.I don't want to show on pagination click.but in Mozilla and chrome this doesn't happen. How to avoid this .
Here I am giving my script
<script>
var warnMessage = "You have unsaved changes on this page!";
$(document).ready(function () {
$('input:not(:button,:submit),textarea,select').change(function () {
window.onbeforeunload = function () {
if (warnMessage != null) return warnMessage;
}
});
$('input:submit').click(function (e) {
warnMessage = null;
});
});
</script>
How to avoid of this message in gridview pagination click in IE only? Can anybody help me out?
Thanks
回答1:
just set the the value to null on pagination click
window.onbeforeunload = null;
UPDATE 1
try this one instead of your code:
var myEvent = window.attachEvent || window.addEventListener; var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; /// make IE7, IE8 compitable myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox var confirmationMessage = 'Are you sure to leave the page?'; // a space (e || window.event).returnValue = confirmationMessage; return confirmationMessage; });
UPDATE 2
after you use window.onbeforeunload = null; in your pagination control to rebind the alert in your update panel try this solution, it may help you with that issue:
$(document).ready(function () { bindPageAlert(); }); function bindPageAlert() { var myEvent = window.attachEvent || window.addEventListener; var chkevent = window.attachEvent ? 'onbeforeunload' : 'beforeunload'; myEvent(chkevent, function (e) { // For >=IE7, Chrome, Firefox var confirmationMessage = 'Are you sure to leave the page?'; // a space (e || window.event).returnValue = confirmationMessage; return confirmationMessage; }); } Sys.WebForms.PageRequestManager.getInstance().add_endRequest(bindPageAlert);
来源:https://stackoverflow.com/questions/24117366/how-to-avoid-the-are-you-sure-you-want-to-navigate-away-from-this-page-when-c