I've made a function to give a warning when a user navigates away when a certain form field is populated:
window.addEventListener("beforeunload", function (event) {
var unsaved = "Are you sure you want to exit?";
var text = $('.article_div textarea').val();
if (text.length > 0){
event.returnValue = unsaved;
return unsaved;
}
});
This actually works perfectly. However, I looked up returnValue and it is recommended to not use it. I have tried alternatives including removing event.returnValue = unsaved;
and just using return unsaved
. However, that also doesn't work - it still navigates away from the page.
Any ideas as to what I can do to make it work?