js运行环境判断
一、前言
? ? 在閱讀vue2.5+源碼時,看到了vue的環境判斷的代碼,整理總結一下,方便大家在運行環境判斷時進行使用。
二、正文
1、判斷瀏覽器運行環境
const inBrowser = typeof window !== 'undefined'2、IE環境
const UA = inBrowser && window.navigator.userAgent.toLowerCase() const isIE = UA && /msie|trident/.test(UA) //IE const isIE9 = UA && UA.indexOf('msie 9.0') > 0 //IE9+ const isEdge = UA && UA.indexOf('edge/') > 0 //edge3、Android環境
const UA = inBrowser && window.navigator.userAgent.toLowerCase() const isAndroid = (UA && UA.indexOf('android') > 0)4、IOS環境
const UA = inBrowser && window.navigator.userAgent.toLowerCase() const isIOS = (UA && /iphone|ipad|ipod|ios/.test(UA))5、chrome運行環境
const UA = inBrowser && window.navigator.userAgent.toLowerCase() const isEdge = UA && UA.indexOf('edge/') > 0 //edge const isChrome = UA && /chrome\/\d+/.test(UA) && !isEdge三、總結
? js運行環境的判斷主要依賴與 window.navigator.userAgent所返回的環境string。
總結
- 上一篇: Lua 在Windows下的运行环境搭建
- 下一篇: css设置背景图片等比例铺满整个页面