jquery - .NET MVC - "beforeunload" event with server-side check -
this question has answer here:
- jquery promise wait ajax end 3 answers
in mvc application, when user leaves page need prompt them warning so:
$(window).on("beforeunload", function () { return "you lose changes, sure?" });
this jquery works, first need check whether user should warned in first place. check complex , needs performed server side (i've struggled find example of this). possible?
i've tried ajax post:
$(window).on("beforeunload", function () { $.ajax({ type: "post", url: "@url.action("savecheck")", async: false success: function (result) { //return string here if saving required } }); });
but doesn't work above - "beforeunload" function returns , doesn't care result of ajax post.
is there way can use calculated flag server check whether user should warned or not before exit?
this answer looks relevant, not allow use result of ajax call in "beforeunload" function, therefore can't warn user: window.onbeforeunload ajax request in chrome
you need wait promise returned ajax call complete. see answer: jquery promise wait ajax end
Comments
Post a Comment