How to run a javascript using href?

Asked

Viewed 97 times

1

I would like to run the javascript below using href.
But by clicking on the supposed link Log Out, nothing happens. If I hold the cursor over the link it shows me the destination as javascript:logout()

function logout() {
    var choose = confirm("Deseja realmente sair?");
    if (choose == true) {
        location="Main.html";
    } else {
			
    }
<nav id="menuOp"> 
    <a href="javascript:logout()">Log Out</a></br>
    <b>Olá, #</b>
</nav>

  • Do you really have to use href? For what reason?

  • 3

    When a javascript code does not work, open the browser console and see if it prints out any error messages. If what happened is what @Joaopaulo said, probably allgum error was spat on the console and you wouldn’t even need to have created that question...

  • 1

    You opened it up function logout() { but did not close with }, and a typo.

1 answer

6


In your script failed to close the logout function

logout(){ 
   //conteudo
}

You can also exchange href for onclick:

<a onclick="javascript:logout()">Log Out</a>
  • Thank you so much for seeing this mistake that went unnoticed and the onclick tip

Browser other questions tagged

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