简单获取系统版本:
let webLog = {} let userAgent = navigator.userAgent // 苹果手机 if (userAgent.includes('iPhone') || userAgent.includes('iPad')) { // 获取操作系统版本 let m1 = userAgent.match(/iPhone OS .*?(?= )/) if (m1 && m1.length > 0) { let num = m1[0].match(/d+/g)[0]; if (parseInt(num) === 9) { return true; } } }
全面版本如下:
// 获取系统版本 function getOsVersion() { var u = navigator.userAgent, version = '' if (u.indexOf('Mac OS X') > -1) { // ios var regStr_saf = /OS [d._]*/gi var verinfo = u.match(regStr_saf) version = 'IOS' + (verinfo + '').replace(/[^0-9|_.]/ig, '').replace(/_/ig, '.') } else if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) { // android version = 'Android' + u.substr(u.indexOf('Android') + 8, u.indexOf(';', u.indexOf('Android')) - u.indexOf('Android') - 8) } else if (u.indexOf('BB10') > -1) { // 黑莓bb10系统 version = 'blackberry' + u.substr(u.indexOf('BB10') + 5, u.indexOf(';', u.indexOf('BB10')) - u.indexOf('BB10') - 5) } else if (u.indexOf('IEMobile') > -1) { // windows phone version = 'winphone' + u.substr(u.indexOf('IEMobile') + 9, u.indexOf(';', u.indexOf('IEMobile')) - u.indexOf('IEMobile') - 9) } else { var userAgent = navigator.userAgent.toLowerCase() if (userAgent.indexOf('windows nt 5.0') > -1) { version = 'Windows 2000' } else if (userAgent.indexOf('windows nt 5.1') > -1 || userAgent.indexOf('windows nt 5.2') > -1) { version = 'Windows XP' } else if (userAgent.indexOf('windows nt 6.0') > -1) { version = 'Windows Vista' } else if (userAgent.indexOf('windows nt 6.1') > -1 || userAgent.indexOf('windows 7') > -1) { version = 'Windows 7' } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows 8') > -1) { version = 'Windows 8' } else if (userAgent.indexOf('windows nt 6.3') > -1) { version = 'Windows 8.1' } else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows nt 10.0') > -1) { version = 'Windows 10' } else { version = 'Unknown' } } return version }