How to control the event to leave the page?

2

I put an event using JavaScript in a <a> tag that is styled as a button, the event is only to show a message that the user will exit the page. But the message looks like all <a> tags, and I want it to ask only when the user clicks back .

    <div>
        <div class="groupb">
            <a href="../cadastrarPaciente.php" class="botoes">Cadastrar</a>
        </div>

        <div class="groupb">
            <a href="../consultarPaciente.html" class="botoes">Consultar</a>
        </div>

        <div class="groupb">
            <a href="../compararPaciente.html" class="botoes">Comparar</a>
        </div>

        <div>
            <form action="#" method="POST" enctype="multipart/form-data">
                <input type="file" name="fileUpload">
                <input type="submit" value="Enviar">
            </form>
        </div>

        <div class="groupb">
            <a href="../index.html" onclick="confirmaSaida()" class="botoes">voltar</a>
        </div>
    </div>
    
asked by anonymous 08.11.2017 / 04:35

1 answer

2

To accomplish this function you need to implement the confirmaSaida function, calling confirm to verify that the user really wants to exit, and if so, you redirect to the page you want by setting window.location to the link page.

function confirmaSaida() {
  var option = confirm("Você realmente deseja sair?\nPara sair clique em OK");
  
  if(option) {
    /* Redireciona para a página index */
    window.location = '../index.html'
  }
}
<div>
    <div class="groupb">
        <a href="../cadastrarPaciente.php" class="botoes">Cadastrar</a>
    </div>

    <div class="groupb">
        <a href="../consultarPaciente.html" class="botoes">Consultar</a>
    </div>

    <div class="groupb">
        <a href="../compararPaciente.html" class="botoes">Comparar</a>
    </div>

    <div>
        <form action="#" method="POST" enctype="multipart/form-data">
            <input type="file" name="fileUpload">
            <input type="submit" value="Enviar">
        </form>
    </div>

    <div class="groupb">
        <a href="#" onclick="confirmaSaida()" class="botoes">voltar</a>
    </div>
</div>
    
08.11.2017 / 11:21