How to control the event to get out of the page?

Asked

Viewed 283 times

2

I put an event using Javascript in a tag <a> that this stylized as a button, the event serves only to show a message that the user will leave the page. But the message looks for all tags <a>, and I want him to ask only when the user clicks come 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>
  • Edith your question and put the code Javascript !!

  • If you ask for all "a" then you must be using bind of some function in the "a" tags. From the above code until now the only "a" tag that calls a function is the link back using onclick.

1 answer

2

To perform this function you need to implement the function confirmaSaida, calling for confirm to check if the user really wants to quit, and in positive case you redirect to the page you want, setting window.location to the link on this 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>

Browser other questions tagged

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