简体   繁体   中英

JS Redirect based on browser language if ... else' statement loops infinitely

I've written a tiny piece of code to redirect based on browser language, but the 'else' portion of the code loops infinitely if I use the original URL.

I have tested it where if I redirect to a different URL it works like a charm, so I am assuming it has something to do with trying to redirect it back to the original URL but not sure how to stop this. Break/Continue and console.log works but the first two are illegal and obviously I can't leave console.log in :SI need to tell the if/else statement to essentially stop or "stay on page" if the first condition is false.

Can someone tell me how to fix this so that if the browser language is set to French, redirect ELSE stay on original URL?

<!-- language redirect -->
    <script>
        userLang = navigator.language || navigator.userLanguage;

    if (userLang == "fr") {
        window.location.href = "http://www.website.com/fr";

    }
    else {
        console.log('null'); *** THIS WORKS but is not right

window.location.href = "http://www.website.com"; *** THIS DOESN'T WORK gets stuck in a loading loop infinitely

window.location.href="http://www.someotherwebsite.com"; ***THIS WORKS but doesn't achieve my purpose
}
</script>

If the browser language is French, redirect to French website (subdirectory) ELSE do nothing and stay on English site.

Since this script is only going to be implemented in the English page, you do not need to pass anything for the else statement.

userLang = navigator.language || navigator.userLanguage;

if (userLang == "fr") {
    window.location.href = "http://www.website.com/fr";
}

For the French page, you just need to change "fr" to "en".

When you assign a url to the window.location.href property the url specified will get loaded even if it's set to the url you are currently on. This causes your browser to constantly reload your website if the language is not french.

From MDN :

Whenever a new value is assigned to the location object, a document will be loaded using the URL as if location.assign() had been called with the modified URL. Note that security settings, like CORS, may prevent this to effectively happen.

To solve your problem, you should not set the window.location.href property when you are already on the correct page for the users language.

This is just a logical issue. Why redirect to a url if it is already in there ? Just have an extra check, whether it is already in the right url as below

<script>
        userLang = navigator.language || navigator.userLanguage;

    if (userLang == "fr" && window.location.href!= "http://www.website.com/fr") {

        window.location.href = "http://www.website.com/fr";

    }
    else {

       if(window.location.href != "http://www.website.com") {
             window.location.href = "http://www.website.com";
       }

    }

</script>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM