background

For many H5 projects, it is necessary to determine which device terminal and browser environment the host environment is in, such as the sharing of wechat and Weibo. You need js to determine which browser the current page is opened in.

const browser = function() { const ua = navigator.userAgent; const appVersion = navigator.appVersion; const version = { isIos: !! ua.match(/\(i[^;] +; ( U;) ? +Mac OS X/), // ios Terminal //isMobile:!! Ua. Macth (/ (iPhone | iPod | the | Android | ios)/I), / / whether the mobile terminals isMobile:!!!!! Ua. Match (/AppleWebKit.*Mobile.*/), // isAndroid: Ua. IndexOf (' Android ') > 1 | | ua. The indexOf (' Linux ') > 1, or uc browser / / / / Android terminal end isAndroid:!!!!! appVersion.match(/android/gi), isIphone: !! Ua.indexof ('iPhone') > -1, // iS it iPhone? // isIphone:!! appVersion.match(/iphone/gi); IsWebApp: uA.indexof ('Safari') === -1, // isWebApp: ua.indexof ('Safari') === -1, // isTrident: Ua.indexof ('Trident') > -1, // isPresto: ua.inexof ('Presto') > -1, // Opera isWebKit: Ua.indexof ('AppleWebkit') > 1, // isGecko: Ua. IndexOf (' Gecko ') > 1 && u.i ndexOf (' KHTML ') = = = 1, / / firefox kernel isWechat:!!!!! ua.match(/micromessenger/gi), isWeiBo: !! ua.match(/weibo/gi), isQQ: !! ua.match(/qq/gi), } const language = (navigator.browserLanguage || navigator.language).toLowerCase(); return { version, language } }Copy the code