【发布时间】:2015-02-13 17:47:20
【问题描述】:
我在哪里可以找到一个简单的 javascript 重定向代码,它将检测到的所有语言重定向到英文页面,但有其正确页面的意大利语除外?
非常感谢
【问题讨论】:
我在哪里可以找到一个简单的 javascript 重定向代码,它将检测到的所有语言重定向到英文页面,但有其正确页面的意大利语除外?
非常感谢
【问题讨论】:
这样的?
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;
}
}
一般来说,我更喜欢在服务器级别进行重定向(例如,使用 mod_rewrite,用于 Apache):
http://www.giuseppeurso.eu/en/url-redirection-according-to-browser-language-apache-mod_rewrite/
PS:这里有一些附加链接,根据语言提供不同的重定向选项:
http://moz.com/community/q/best-practice-to-redirects-based-on-visitors-detected-language
HTML 重定向
JavaScript 重定向
Apache 重定向
Nginx 重定向
Lighttpd 重定向
PHP 重定向
Ruby on Rails 重定向
.NET 重定向
Node.js 重定向
【讨论】: