JS来判断网页是在什么端打开

利用正则表达式判断手机端还是PC端

<script type="text/javascript">
	function isPC() {
           var userAgent = navigator.userAgent.toLowerCase();
           if (/ipad|iphone|midp|rv:1.2.3.4|ucweb|android|windows ce|windows mobile/.test(userAgent)) {
               //跳转移动端页面
               window.location.href="http://a.name.com/mobile/index.html";
           } else {
               //跳转pc端页面
               window.location.href="http://b.name.com/pc/index.html";
           }
       }
</script>

获取移动端设备

<script type="text/javascript">
    function getOs() {
        var os = ''
        var userAgent = window.navigator.userAgent.toLowerCase();
        if (!!userAgent.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/)) {
            // ios端
            os = 'ios'
        }
        else if (userAgent.indexOf('android') > -1 || userAgent.indexOf('Adr') > -1) {
            // android端
            os = 'android'
        }
        if (userAgent.match(/WeiBo/i) == "weibo") { // 微博
            os = 'wb'
        }
        if (userAgent.match(/MicroMessenger/i) == 'micromessenger') {
            // 微信浏览器
            os = 'wx'
        }
        if (userAgent.match(/QQ/i) == "qq") {
            //在QQ空间打开
            os = 'qq'
        }
        alert(os)
        return os
    }
</script>
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值