1
I am trying to run the function below so that when the user closes the page, an alert will appear asking if you want to save before leaving, by clicking on OK
, the progress of the form will be saved in the localStorage
and by clicking on Cancelar
, the window will be closed as the user requested, but it is not working at all, what is wrong? It is possible to do this?
Code:
<form id="msform">
<input id="um" /><br>
<input id="dois" />
</form>
window.onbeforeunload = saida;
function saida(){
var sair = confirm("Deseja salvar antes de sair?");
if (sair == true) {
[].forEach.call(document.querySelector('#msform').elements, function(el) {
localStorage.setItem(el.id, el.value);
});
} else {
window.close();
}
}
Read this answer I had made a while ago, will understand how the
onbeforeunload
works.– Alex