How to create validation to ask if the user really wants to leave the page? [duplicate]

3

I have a page where the user will edit fields that are in a form. This data comes from the database. The user can edit them, however, in the end he can write (that is, he will write / do update in the database) or he can close.

I want to make sure that if the user clicks on another menu link or clicks back, the user is prompted to leave the page without pressing the save changes button

    
asked by anonymous 29.09.2016 / 13:54

1 answer

2

Create a method to confirm the output of the page, passing the url as a parameter:

function confirmar(url){
    event.preventDefault();  
    var resposta = confirm("Deseja mesmo sair da página?");
    if (resposta == true){
        location.top.href = url;
    }
}    
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

Then put in the onclick attribute of the html tag.

    
29.09.2016 / 14:29