Outbound confirmation message

0

I'm working with document creation, where the user enters the information so that the document can be generated. I would like that if the user had already typed something in one of the fields and clicked to exit the page, an exit confirmation message appeared on the screen. I gave a read on other topics here and found onbeforeunload of JavaScript , however, I did not find anything talking about how to make the message appear only if there is something in the fields. My code looks like this:

<script language="JavaScript">
    window.onbeforeunload = confirmExit;
    function confirmExit(){
        if((document.getElementsByName("interessado").value != "")||
           (document.getElementsByName("assunto").value != "")||
           (document.getElementsByName("assinatura").value != "")){
                return "Deseja realmente sair?";
        }
    }

How can I create an output confirmation message only if there is any information in the text fields?

    
asked by anonymous 10.10.2017 / 15:13

1 answer

1

getElementsByName returns an array, so you need to check the index:

function confirmExit(){
    if( ( !document.getElementsByName("interessado")[0].value )||
        ( !document.getElementsByName("assunto")[0].value )||
        ( !document.getElementsByName("assinatura")[0].value )){
            return "Deseja realmente sair?";
    }      
}
    
10.10.2017 / 15:17