判断当前打开的是手机还是pc浏览器还是微信浏览器

57 篇文章 0 订阅

第一种:

// if (
            //     window.navigator.userAgent.match(
            //         /(phone|pad|pod|iPhone|iPod|ios|iPad|Android|Mobile|BlackBerry|IEMobile|MQQBrowser|JUC|Fennec|wOSBrowser|BrowserNG|WebOS|Symbian|Windows Phone)/i
            //     )
            // ) {
            //     // return true; // 移动端
            //     var ua = navigator.userAgent.toLowerCase();
            //     if (ua.match(/MicroMessenger/i) == "micromessenger") {
            //         _this.browserCopy = "weixin";
            //         console.log("weixin");
            //     } else {
            //         _this.browserCopy = "mobile";
            //         console.log("mobile");
            //     }
            // } else {
            //     // return false; // PC端
            //     _this.browserCopy = "pc";
            //     console.log("pc");
            // }

弊端:
在pc端的手机调试状态,他会判定为此时在手机浏览器
第二种:

 //平台、设备和操作系统
            var system = {
                win: false,
                mac: false,
                xll: false,
                ipad: false,
            };
            //检测平台
            var p = navigator.platform;
            system.win = p.indexOf("Win") == 0;
            system.mac = p.indexOf("Mac") == 0;
            system.x11 = p == "X11" || p.indexOf("Linux") == 0;
            system.ipad =
                navigator.userAgent.match(/iPad/i) != null ? true : false;
            
            if (system.win || system.mac || system.xll || system.ipad) {
                _this.browserCopy = "pc";
                console.log("pc");
            } else {
                var ua = navigator.userAgent.toLowerCase();
                if (ua.match(/MicroMessenger/i) == "micromessenger") {
                    _this.browserCopy = "weixin";
                } else {
                    _this.browserCopy = "mobile";
                }
            }

这种方式不会出现上面的问题
在这里插入图片描述

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值