For ASP.NET MVC if you want to make an exception for leaving the page via submitting a particular form:
Set a form id:
@using (Html.BeginForm("Create", "MgtJob", FormMethod.Post, new { id = "createjob" }))
{
// Your code
}
<script type="text/javascript">
// Without submit form
$(window).bind('beforeunload', function () {
if ($('input').val() !== '') {
return "It looks like you have input you haven't submitted."
}
});
// this will call before submit; and it will unbind beforeunload
$(function () {
$("#createjob").submit(function (event) {
$(window).unbind("beforeunload");
});
});
</script>