2
I can’t make it work at all...!
<script>
document.getElementById("ClicouNaTag").addEventListener(
"click",function() {
window.location.href = "vaiparaessapagina.php";
}
);
</script>
2
I can’t make it work at all...!
<script>
document.getElementById("ClicouNaTag").addEventListener(
"click",function() {
window.location.href = "vaiparaessapagina.php";
}
);
</script>
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 var
and 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 javascript html
You are not signed in. Login or sign up in order to post.
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.– Wallace Maxters
Example, if you do
location.href = 'index.php'
on a page like this:exemplo.com/teste/teste
, he’ll redirect like thisexemplo.com/teste/index.php
. Confirm for us.– Wallace Maxters
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
– Emilio Dami Silva
Already checked the requests on the console?
– Wallace Maxters
Note: <li class="Nav-item"><a id="Clicounatag" class="Nav-link" href="index.php"><em class="fa-address-card"></em>Vaiparaessapagina</a>
– Emilio Dami Silva
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
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
– Emilio Dami Silva
take a look at my answer, young man. See if it helps you.
– Wallace Maxters