When I click on the tag reference id it does not call the page I want

Asked

Viewed 47 times

2

I can’t make it work at all...!

<script>
    document.getElementById("ClicouNaTag").addEventListener(
        "click",function() {
            window.location.href = "vaiparaessapagina.php";
        }
    );  
</script>
  • Is the page at the root? Because, when you use vaiparaessapagina.php, if you are in a path that has "sub paths" in the url, it will only change the last path.

  • Example, if you do location.href = 'index.php' on a page like this: exemplo.com/teste/teste, he’ll redirect like this exemplo.com/teste/index.php. Confirm for us.

  • Yes. Yeah, nothing happens...continues on the same page...I tried to put an alert and so the alert enters and shows the message, but the page continues in index.php

  • Already checked the requests on the console?

  • Note: <li class="Nav-item"><a id="Clicounatag" class="Nav-link" href="index.php"><em class="fa-address-card"></em>Vaiparaessapagina</a>

  • Emilio, isn’t it because you already have one href on the tag you’re putting the action? Add that snippet to the question.

  • Wallace Maxters can help me make these queries on the console? I don’t know...I just see that the page is still index.html in the way of the browser

  • take a look at my answer, young man. See if it helps you.

Show 3 more comments

1 answer

1


The Author of the Question informed that it has the following code snippet:

<li class="nav-item">
    <a id="ClicouNaTag" class="nav-link" href="index.php">
        <em class="fa fa-address-card"></em>Vaiparaessapagina</a>

The code for page change is as follows:

document.getElementById("ClicouNaTag").addEventListener(
    "click",function() {
        window.location.href = "vaiparaessapagina.php";
    }
);  

The problem is that the element a#ClicouNaTag has the attribute href that causes the redirect to be done to the page itself. Thus, this redirection will prevent the event click be sued.

It is important to remember that in many cases where we use capture of events in Javascript, we must prevent the standard action of the element through the method event.preventDefault().

Change your code to:

document.getElementById("ClicouNaTag").addEventListener(
    "click",function(event) {
        event.preventDefault();
        window.location.href = "vaiparaessapagina.php";
    }
);  

And since you won’t be using the href="index.php", I would replace that stretch with # or by javascript: void(0).

Thus:

 <a id="ClicouNaTag" class="nav-link" href="#">...
  • It didn’t work. And look here the code: <li class="Nav-item"> <a id="Clicouaqui" class="Nav-link" href="#"></a> </li> <script> Document.getElementById("Clicouaqui"). addeventlistener( Event.preventDefault(); Alert("You clicked here"); window.location.href = "vaiparaessapagina.php"; ); </script> It doesn’t work at all. The ALERT works but does not go to the page in any way.

  • Guys, thank you! I got... I still think of another question: I have a global variable, can I change it inside javascript? Document.getElementById("Clicounatag"). addeventlistener( "click",Function(Event) { Event.preventDefault(); varGloal = '1'; window.location.href = "vaiparaessapagina.php"; } );

  • @Emiliodamisilva if applicable, ask another question, but you can. To change a global variable, simply declare it outside the scope of the function with varand within the function assign a value to it, without declaring with var).

  • @Emiliodamisilva recommend this reading: https://answall.com/questions/2513/quando-se-deve-usar-var-no-javascript

  • Thanks @Wallace Maxters I still don’t know how to grade the answers, but it’s 10!!!!!! Thanks!!!!

Browser other questions tagged

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