婺源旅游网 - 最美乡村 - 婺源民间故事
标题:
自动判断浏览器的中英文版本自动跳转网站中英文页面代码
[打印本页]
作者:
田野
时间:
2021-3-13 14:53
标题:
自动判断浏览器的中英文版本自动跳转网站中英文页面代码
许多网站现在都是依靠自动判断浏览器的中英文版本来判断给你打开网站的中文还是英文版,也许好多朋友还不知道这个代码,今天正好给公司改版企业网站用到了,在此公布出来,供大家一起学习。
HTML网页根据来访者的浏览器语言不同自动跳转多语言页面
也就是自动判断浏览器的中英文版本自动跳转网站中英文页面
在 <head> </head> 之间加入如下代码。
<script>
var type=navigator.appName
if (type=="Netscape")
var lang = navigator.language
else
var lang = navigator.userLanguage
//cut down to first 2 chars of country code
var lang = lang.substr(0,2)
// 英语
if (lang == "en")
window.location.replace('url')
// 简体中文
else if (lang == "zh-cn")
window.location.replace('url')
// 繁体中文
else if (lang == "zh-tw")
window.location.replace('url')
// 德语
else if (lang == "de")
window.location.replace('url')
// 除上面所列的语言
else
window.location.replace('url')
</script>
复制代码
-----------------------------------------------------------华丽的分隔线-----------------------------------------------
根据浏览器自动判断代码:
<script language="JavaScript">
<!--
function getOs()
{
var OsObject = "";
if(navigator.userAgent.indexOf("MSIE")>0) {
return "MSIE";
}
if(isFirefox=navigator.userAgent.indexOf("Firefox")>0){
return "Firefox";
}
if(isSafari=navigator.userAgent.indexOf("Safari")>0) {
return "Safari";
}
if(isCamino=navigator.userAgent.indexOf("Camino")>0){
return "Camino";
}
if(isMozilla=navigator.userAgent.indexOf("Gecko/")>0){
return "Gecko";
}
}
var os = getOs();
if (os=="MSIE")
location.href="/a.htm"
else if ( os=="Firefox" )
location.href="/b.htm"
-->
</script>
复制代码
-----------------------------------------------------------又来的分隔线-----------------------------------------------
根据操作系统自动判断:
<script language="JavaScript">
<!--
var la=navigator.browserLanguage.toLowerCase(); //识别系统语言
if(la=='zh-cn') document.location = 'https://www.wy166.com'; //如果是简体中文
else if(la=='zh-tw') document.location = 'https://tn.wy166.com'; //如果繁体中文
else document.location = 'http://www.wy166.com'; //如果都不是
// -->
</script>
复制代码
欢迎光临 婺源旅游网 - 最美乡村 - 婺源民间故事 (https://www.wy166.com/)
Powered by Discuz! X3.5