您的位置:首页 > 其它

通过浏览器判断显示什么页面

2011-06-09 10:58 239 查看
以下代码经本人在IE6、IE7、Firefox、Opera浏览器测试成功。它的功能是如果你的浏览器是中文版的IE7、Firefox、Opera则不管你输入的域名是中文还是英文都自动转跳到中文域名,如果是中文域名则自动去掉前面的 www.,如果你的浏览器为IE6或更低版本以及其它浏览器,则地址栏显示的是英文域名。
以下代码使用的域名为loongcom.com hotbiz.cn 服务.cn 服务.中国,带www的和不带www的,请自己修改网址,把这段javascript程序放在首页default.html的<head> </head>区域即可

<script language="javascript" type="text/javascript"> ////本程序为龙永超编写,可以随意修改。联系QQ:187029320 使用时请把你的网站空间首页文件默认设置为default.html,网站的真正首页文件为index.html
function support(){
var ua = navigator.userAgent; //通过解析navigator对象的userAgent属性来获得浏览器的完整版本号
var IE = ua.indexOf("MSIE "); // IE将自己标识为MSIE,后面带一个空格,版本号以及分号。所以我们只要取空格和分号之间的部分即可
var name = navigator.appName; //获取浏览器的名称
if (name == 'Netscape') //如果浏览器为Firefox
var l = navigator.language; //定义Firefox浏览器的默认语言
else
var l = navigator.browserLanguage;//定义非Firefox浏览器的默认语言
if((ua.indexOf("Win") != -1) && (IE >= 7) && (l.indexOf('zh') > -1))return true;//windows操作系统、浏览器版本大于7且浏览器默认语言为中文
if ((ua.indexOf("Win") != -1) && (name == "Netscape") && (l.indexOf('zh') > -1))return true;//windows操作系统、浏览器为Firefox且浏览器默认语言为中文
if ((ua.indexOf("Win") != -1) && (name == "Opera") && (l.indexOf('zh') > -1))return true;//windows操作系统、浏览器为Opera且浏览器默认语言为中文
return false;
}

function chinaesedomain(){if(support()){
try {
if( self.location == "http://服务.cn/" ) { top.location.href = "http://服务.cn/index.html"; } //去掉中文域名前面的www.
else if( self.location == "http://www.xn--zfr188b.xn--fiqs8s/" ) { top.location.href = "http://服务.cn/index.html"; }//解决Firefox浏览器地址栏不显示纯中文域名的问题
else if( self.location == "http://xn--zfr188b.xn--fiqs8s/" ) { top.location.href = "http://服务.cn/index.html"; } //解决Firefox浏览器地址栏不显示纯中文域名
else if( self.location == "http://www.xn--zfr188b.cn/" ) { top.location.href = "http://hotbiz.cn/index.html"; }//解决IE6浏览器访问中文.cn域名地址栏显示转码的问题
else if( self.location == "http://xn--zfr188b.cn/" ) { top.location.href = "http://hotbiz.cn/index.html"; } //解决IE6浏览器访问中文.cn域名地址栏显示转码的
else if( self.location == "http://www.服务.中国/" ) { top.location.href = "http://服务.中国/index.html"; }//去掉中文域名前面的www.
else if( self.location == "http://服务.中国/" ) { top.location.href = "http://服务.中国/index.html"; } //去掉中文域名前面的www.
else if( self.location == "http://www.服务.cn/" ) { top.location.href = "http://服务.cn/index.html"; }//去掉中文域名前面的www.
else { top.location.href = "http://服务.cn/index.html"; }//输入其它域名都自动转跳到中文域名
}
catch(e) { }

}else{
try {
if( self.location == "http://www.xn--zfr188b.cn/" ) { top.location.href = "http://hotbiz.cn/index.html"; }//解决IE6浏览器访问中文.cn域名地址栏显示转码的问题
else if( self.location == "http://xn--zfr188b.cn/" ) { top.location.href = "http://hotbiz.cn/index.html"; } //解决IE6浏览器访问中文.cn域名地址栏显示转码的
else { top.location.href = "index.html"; } //非IE7浏览器为IE6或更低版本以及其它浏览器,则地址栏显示的英文域名不变
}
catch(e) { }
}
}

chinaesedomain();
</script>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: