Reputation: 311
Where can i find a simple javascript redirect code that redirect all the language detected to an english page, except the italian language that has its proper page?
Thank you very much
Upvotes: 2
Views: 3312
Reputation: 121609
Something like this?
http://javascript.about.com/library/bllang.htm
var langcodes=["fr", "es"];
// Browser Language Redirect script
// copyright 3rd January 2006, Stephen Chapman
// permission to use this Javascript on your web page is granted
// provided that all of the code in this script (with the sole exception
// of the langcodes array entries) is used without any alteration
var langCode = navigator.language || navigator.systemLanguage;
var lang = langCode.toLowerCase();
lang = lang.substr(0,2);
var dest = window.location.href;
for (i=langcodes.length-1;i >= 0; i--){
if (lang==langcodes[i]){
dest = dest.substr(0,dest.lastIndexOf('.')) + '-' + lang.substr(0,2) + dest.substr(dest.lastIndexOf('.'));
window.location.replace ?window.location.replace(dest) :window.location=dest;
}
}
Generally, I'd prefer to redirect at the server level (e.g. with mod_rewrite, for Apache):
http://www.giuseppeurso.eu/en/url-redirection-according-to-browser-language-apache-mod_rewrite/
PS: Here are some additional links, with different alternatives for redirecting based on language:
http://moz.com/community/q/best-practice-to-redirects-based-on-visitors-detected-language
HTML redirects
JavaScript redirects
Apache redirects
Nginx redirects
Lighttpd redirects
PHP redirects
Ruby on Rails redirects
.NET redirects
Node.js redirects
Upvotes: 2