根据设备及设备语言去访问不同的网址。(核心:navigator.userAgent、navigator.language)

本文介绍了一种用于判断移动设备并提供相应访问优化的JavaScript代码,同时详细解释了如何通过`navigator.language`和`navigator.browserLanguage`获取浏览器语言,并提供了不同语言环境下网站访问的指引。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >



<script type="text/javascript">


        window.onload = function() {

            if (window.location.href.indexOf('#') > -1) {
                return true;
            }

            var browserLanguage = (navigator.language || navigator.browserLanguage).toLowerCase();
            var userAgent = navigator.userAgent.toLowerCase();
            var url;
            var tip;

            var android = userAgent.indexOf('android') > -1;
            var iphone = userAgent.indexOf('iphone') > -1;
            var ipad = userAgent.indexOf('ipad') > -1;
            var ipod = userAgent.indexOf('ipod') > -1;
            var windows_phone = userAgent.indexOf('windows phone') > -1;
            var blackberry = userAgent.indexOf('blackberry') > -1;
            var symbia = userAgent.indexOf('symbia') > -1;
            var nokiabrower = userAgent.indexOf('nokiabrower') > -1;


            if (browserLanguage.indexOf('zh') > -1) {
                url = "http://吕津.net/Category_73/Index.aspx";
                tip = "系统检测到您目前正在使用移动设备,建议您访问我们的移动版平台。";
            }
            else if (browserLanguage.indexOf('en') > -1) {
                url = "http://www.吕津.net/Category_174/Index.aspx";
                tip = "You are using mobile device, please visit our mobile platform.";
            }
            else if (browserLanguage.indexOf('fr') > -1) {
                url = "http://www.吕津.net/Category_266/Index.aspx";
                tip = "Vous êtes en train d'utiliser l'appareil mobile, veuillez visiter notre plate-forme mobile.";
            }
            else {
                url = "http://www.吕津.net/index.html#";
                tip = "You are using mobile device, please visit our mobile platform.";
            }

            if (android || iphone || ipad || ipod || windows_phone || blackberry || symbia || nokiabrower) {
                if (confirm(tip)) {
                    location.href = "http://m.吕津.net";
                }
                else {
                    location.href = url;
                }
            }
            else {
                location.href = url;
            }


        }
       

    </script>

PS:

(1)

上面判定移动设备是通过HTTP头的userAgent,枚举目前市面上常见的移动设备。

可以参考我的博文:userAgent一览: http://blog.csdn.net/lvjin110/article/details/12944397

(2)

至于为什么获取浏览器语言要使用

navigator.language || navigator.browserLanguage

可以参考我的另外一篇博文:获取浏览器语言的完美方案。(各浏览器对 navigator 对象中几个与语言相关的属性的返回值存在差异)

http://blog.csdn.net/lvjin110/article/details/37663067




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值