3

I hope you will be able to help me with my problem. I have 3 languages on my static html site. I would like to redirect a user based on their browser language settings.

This is my code:

    var lang = navigator.language;
if (lang == 'pl'){
    document.location.href = 'index_pl.html';
}
else if (lang == 'fr'){
    document.location.href = 'index_fr.html';
}
else {
    document.location.href = 'index.html';
}
alert(lang); 

The problem is every time user will enter the website this script keeps refreshing/redirect a site. My question is how to check the user browser once and then redirect user to a dedicated webpage.

Thanks in advance.

2 Answers 2

2

If you don't want it to keep redirecting, you need to check for the page in the current URL. For example:

var lang = navigator.language;
var redirectURL = 'index.html';

if (lang == 'pl'){
    redirectURL = 'index_pl.html';
} else if (lang == 'fr'){
    redirectURL = 'index.html';
}

if (document.location.pathname.indexOf(redirectURL) == -1) {
    document.location.href = redirectURL;
}

This checks that the redirectURL is not in the path.

Sign up to request clarification or add additional context in comments.

6 Comments

Thanks for your answer. All files are in root direcory and code above is checking what is the user browser language and based on that information redirects to specific page. Once the user is redirected the scipt check the user language again. (infinite loop). Maybe i should add something to the url after first redirection or maybe use localstorage?
You can use localstorage or cookies if you want to store what the option was, however you shouldn't need to. If its still redirecting, something is not matching with the path check. Comment out the document.location.href and output (alert or console.log) what it shows. For example: alert(document.location.path+" != /"+redirectURL)
returns Undefinded != /index_pl.html But all files are in the same root diretory
Sorry my fault. It's pathname. See revised above.
That may work on the server but on the localhost will return E:/filelocation/foldername/index.html != /index_pl.html I think i will have to find other solution ....
|
0

I think I have solved this issue using localStorage. Code below:

if (localStorage.getItem("visit") == null)
{
    // Show Welcome message
    var lang = navigator.language;
    if (lang == 'pl'){
        document.location.href = 'index_pl.html';
    }
    else if (lang == 'fr'){
        document.location.href = 'index_fr.html';
    }
    else {
        document.location.href = 'index.html';
    }
}

localStorage.setItem("visit", new Date());

1 Comment

The problem with this solution is that you are setting the date after it is set to redirect. I still don't think you need this. You can see my revised solution for checking for the instance of redirectURL in the string, so it will work on your localhost. This type of method works too, but you need to set the item before the redirects. You could put as the first item inside the if statement.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.