Casey Crookston Casey Crookston - 4 days ago 5
Javascript Question

Prevent 'Confirm Navigation' alert with window.location.href

I've got a button:

<button type="button" class="btn btn-primary mt15 send-emails" name="SendEmails" value="true">Send Emails</button>


And a method:

$('.send-emails').click(function (e) {
// build data and other stuff
$.post('/leads/sendemails', data, function (data, status, xhr) {
alert('Emails have successfully been queued for sending.');
window.onbeforeunload = null;
window.location.href = '/Leads/Lead/' + @Model.LeadID;
}, 'json');
});


Even with
window.onbeforeunload = null;
I still get the popup warning:

enter image description here

How can I prevent this?

Answer

The solution is following:

$(window).unbind();

This will remove the onbeforeunload event just before the event is triggered.

Comments