浏览器代理判断丨技术开发分享录

浏览器代理判断

{{ detail.nickname }}

转载 翻译 {{ formatTime(detail.create_time) }} 字数 {{ detail.content && detail.content.length }} 阅读 {{ detail.read_num }} {{ formatTag(v) }}

"## 一、判断微信/微博/QQ\n\n~~~\nvar ua = navigator.userAgent.toLowerCase();\nif(\n\tua.match(/MicroMessenger/i)==\"micromessenger\" || \n    ua.match(/WeiBo/i)==\"weibo\" || \n    ua.match(/QQ/i)==\"qq\"\n) {\n    if(/iPhone/.test(navigator.userAgent)) {\n        shadeLayerEle.querySelector('.js-browser-text').textContent = 'Safari';\n    } else {\n        shadeLayerEle.querySelector('.js-browser-text').textContent = '浏览器';\n    }\n    shadeLayerEle.style.display = 'block';\n}\n~~~\n\n## 二、判断终端\n\n~~~\nvar browser = {\n    versions: function() {\n        var u = navigator.userAgent, app = navigator.appVersion;\n        return {\n            trident: u.indexOf('Trident') > -1,                                     // IE内核\n            presto: u.indexOf('Presto') > -1,                                       // opera内核\n            webKit: u.indexOf('AppleWebKit') > -1,                                  // 苹果、谷歌内核\n            gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1,             // 火狐内核\n            mobile: !!u.match(/AppleWebKit.*Mobile.*/) || !!u.match(/AppleWebKit/), // 是否为移动终端\n            ios: !!u.match(/\\(i[^;]+;( U;)? CPU.+Mac OS X/),                        // ios终端\n            android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1,          // android终端或者uc浏览器\n            iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1,              // 是否为iPhone或者QQHD浏览器\n            iPad: u.indexOf('iPad') > -1,                                           // 是否iPad\n            webApp: u.indexOf('Safari') == -1                                       // 是否web应该程序,没有头部与底部\n        };\n    }(),\n    language: (navigator.browserLanguage || navigator.language).toLowerCase()\n};\nif (browser.versions.ios || browser.versions.iPhone || browser.versions.iPad) {\n    window.location.href = 'download/download_IOS.html';\n}else if (browser.versions.android) {\n    window.location.href = 'download/download_Andriod.html';\n}\n~~~\n\n## 三、判断移动端/PC端\n\n```\nif(/Android|webOS|iPhone|iPod|BlackBerry|ipad/i.test(navigator.userAgent)) {\n\tconsole.log('移动端');\n} else {\n\tconsole.log('PC端');\n}\n```"
PS:写作不易,如要转裁,请标明转载出处。

如果此篇对您有帮助,可小额赞助,以兹鼓励!

猜你想看