Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript redirection according to browser language [duplicate]

I'm trying to send visitors that have browser language in English to an alternative site. I was able to find this code but it's not working:

<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'm not even getting the alert :/
You can see it in http://socialpos.com.ar

like image 846
Dante Pereyra Avatar asked Jul 01 '14 21:07

Dante Pereyra


People also ask

How to redirect a URL?

Click the URL Redirects tab. In the upper right, click Add URL redirect. In the right panel, select the Standard or Flexible redirect type. A standard redirect is used to redirect one URL to another.

What are JavaScript redirects?

A JavaScript redirect is a piece of JavaScript code that is used to automatically transfer a visitor from a landing page to a different target page.

Does Google crawl redirects?

Does Google crawl and index redirects? No, it does not. This means that if you redirect from one page to another, the content on the original page will not get indexed. Only the target URL will be crawled and indexed by the search engine.

How do I redirect my browser language?

To enable browser language redirect, go to WPML → Languages page, and find the Browser language redirect. The default is to not redirect visitors. If you choose to redirect visitors, you can have two options: Only redirect visitors if translations exist.


1 Answers

var language = navigator.browserLanguage;

should be

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

see my console results:

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

Also please note that this was the first result for a google search: "javascript browser language". Google is your friend, and your google-fu is weak. Train it with searches!

like image 177
Sterling Archer Avatar answered Nov 15 '22 22:11

Sterling Archer