分享web开发知识

注册/登录|最近发布|今日推荐

主页 IT知识网页技术软件开发前端开发代码编程运营维护技术分享教程案例
当前位置:首页 > 运营维护

JS 判断各种设备,各种浏览器

发布时间:2023-09-06 02:02责任编辑:林大明关键词:浏览器

话不多说,直接看代码

1、区分Android、iphone、ipad:

var ua = navigator.userAgent.toLowerCase();if (/android|adr/gi.test(ua)) { ???// 安卓 ????}else if(/\(i[^;]+;( U;)? CPU.+Mac OS X/gi.test(ua)){ ???//苹果 ????}else if(/iPad/gi.test(ua)){ ???//ipad }

2、区分设备:  新浪微博为1,QQ客户端为2,微信低于6.0.2版本为3,高于6.0.2版本为4,其他为0。

var ua = navigator.userAgent.toLowerCase(); ?if(ua.match(/weibo/i) == "weibo"){ ?????console.log(1);}else if(ua.indexOf(‘qq/‘)!= -1){ ?????console.log(2);}else if(ua.match(/MicroMessenger/i)=="micromessenger"){ ?var v_weixin = ua.split(‘micromessenger‘)[1]; ?????v_weixin = v_weixin.substring(1,6); ?????v_weixin = v_weixin.split(‘ ‘)[0]; ?if(v_weixin.split(‘.‘).length == 2){ ?????v_weixin = v_weixin + ‘.0‘; ?} ?if(v_weixin < ‘6.0.2‘){ ?????console.log(3);}else{ ?????console.log(4); ?} ?}else{ ?????console.log(0); } 

3、区分各个浏览器

 var ua=navigator.userAgent.toLowerCase(); ?????if(/msie/i.test(ua) && !/opera/.test(ua)){ ?????????alert("IE"); ?????????return ; ?????}else if(/firefox/i.test(ua)){ ?????????alert("Firefox"); ?????????return ; ?????}else if(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua)){ ?????????alert("Chrome"); ?????????return ; ?????}else if(/opera/i.test(ua)){ ?????????alert("Opera"); ?????????return ; ?????}else if(/iPad/i){ ????????alert("ipad"); ????????return ; ????}else if(/webkit/i.test(ua) &&!(/chrome/i.test(ua) && /webkit/i.test(ua) && /mozilla/i.test(ua))){ ?????????alert("Safari"); ?????????return ; ?????}else{ ?????????alert("unKnow"); ?????}

JS 判断各种设备,各种浏览器

原文地址:https://www.cnblogs.com/chenhaoyu/p/9247217.html

知识推荐

我的编程学习网——分享web前端后端开发技术知识。 垃圾信息处理邮箱 tousu563@163.com 网站地图
icp备案号 闽ICP备2023006418号-8 不良信息举报平台 互联网安全管理备案 Copyright 2023 www.wodecom.cn All Rights Reserved