Javascript redirection according to browser language

I am trying to send visitors who have a browser language in English to an alternative site. I managed to find this code, but it does not work:

<script type="type/javascript">

var language = navigator.browserLanguage;

// alert(language);

if (language.indexOf('en') > -1) {
document.location.href = 'http://en.socialpos.com.ar';
} else {
document.location.href = 'http://socialpos.com.ar';
}
</script>

I don't even get the warning: /
You can see it at http://socialpos.com.ar

+4
source share
2 answers
var language = navigator.browserLanguage;

it should be

var language = navigator.language || navigator.browserLanguage; //for IE

see the results of my console:

var language = navigator.browserLanguage;
undefined
language;
undefined
var language = navigator.language;
undefined
language;
"en-US"

Also note that this was the first google search result: “JavaScript Browser Language”. Google is your friend, and your google-fu is weak. Train him with a search!

+8
source

if yours has alert(language)not been commented out, the script tag should be

<script type="text/javascript"></script>

or

<script></script>
+5

Source: https://habr.com/ru/post/1546748/


All Articles