4
Good afternoon!
For example, I made a script to redirect all people from my site to the home of the site, because, we will change system and with this, sales have to stop so we can do this.
I made a mini function just for testing and to be more organized. (I had no need to create function)
However, when I tested it, it redirects the person to the home of the site, which is the root in the case (/) but it at the root is also redirecting and forms an infinite loop, since it is in the url that is actually to be redirected, (only with the function)
let newRedirect = function(){
if(window.location.href != "https://www.meusite.com.br/"){
window.location.href = "https://www.meusite.com.br/";
}
}
newRedirect();
Now, if I use like this (no function):
if(window.location.href != "https://www.meusite.com.br/"){
window.location.href = "https://www.meusite.com.br/";
}
Works normally and redirects only once.
In short, why with function, even at the root, is redirecting and without function, no?
The
location.pathname
returns only the path, so you can validate with a!= "/"
.– Francisco
This shouldn’t happen. Put a
alert(window.location.href)
at the beginning of the function and see what shows.– Sam
You can also try using a.log console (window.location.href) so it’s easier to track and understand what’s going on, what the value is window.location.href...
– Raphael Godoi
Are you using Node.js? And the http-server module?
– user148747
Suddenly it’s a problem with Hoisting?
– Onilol
may be that your Location.href is returning h ttps://meusite.com.br/ without www
– user60252