Warning message when leaving the page without saving

Asked

Viewed 15 times

-2

I have this code that shows a warning as soon as the user changes screen, but I need it to appear only if the user makes any changes to textarea.

What I’ve done so far.

window.addEventListener("beforeunload", function(event) { 
    event.preventDefault();

    event.returnValue = "Deseja sair?"; 
    return "Deseja sair?";
});
  • 2

    How are you doing to check if something has changed? And what kind of things can be changed? Normally it would only be by one if (alterado) { return "Deseja.... or even if (valororiginal != valoratual) { return "Deseja....inside the Listener, but only you know what characterizes a change in your specific case. It is worth noting that inputs in general has the property defaultValue, then do something like if (element.value != element.defaultValue) is the simplest way and compatible with most scenarios in static HTML.

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.