Different behavior inside and outside the function

4

Good afternoon!

For example, I made a script to redirect all the people of my site to the Home of the site, because we will change the system and with that, sales have to stop so we can do that.

I did a mini function just for the test and to be more organized. (no need to create function)

However, when I test it, it redirects the person to Home from the site, which is the root in the case (/), but it also redirects to the root and forms an infinite loop, since it is in the url that is actually to be redirected, (function only)

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 (without function):

if(window.location.href != "https://www.meusite.com.br/"){
    window.location.href = "https://www.meusite.com.br/";
}

It works normally and redirects only once.

In short, why with function, even at the root, is redirecting and no function, right?

    
asked by anonymous 18.09.2018 / 20:53

0 answers