function isSafari(){ var ua = navigator.userAgent; //先排除opera if(!/OPR/.test(ua)){ //检测出chrome和safari浏览器 if(/Safari/.test(ua)){ //检测出safari if(/Version\/(\S+)/.test(ua)){ return RegExp["$1"]; } } } } console.log(isSafari());//只有safari会返回版本号5.1.7,其他浏览器都返回undefined
【4】firefox
function isFireFox(){ if(/Firefox\/(\S+)/.test(navigator.userAgent)){ return RegExp["$1"]; } } console.log(isFireFox());//只有firefox会返回版本号40.0,其他浏览器都返回undefined
【5】opera
function isOpera(){ if(/OPR\/(\S+)/.test(navigator.userAgent)){ return RegExp["$1"]; } } console.log(isOpera());//只有opera会返回版本号32.0.1948.25,其他浏览器都返回undefined
识别操作系统
使用navigator.platform检测操作系统更加简单,因为其可能包括的值为“Win32”、“Win64”、“MacPPC”、“MacIntel”、“X11”和"Linux i686"等,且在不同浏览器中是一致的
而通过navigator.userAgent可以来得到window系统的详细信息
windows版本 -> 内核版本 Windows XP -> 5.1 Windows Vista -> 6.0 Windows 7 -> 6.1 Windows 8 -> 6.2 Windows 8.1 -> 6.3 Windows 10技术预览版 -> 6.4 Windows 10.0 -> 10.0
function whichSyStem(){ var ua = navigator.userAgent; var pf = navigator.platform; if(/Mac/.test(pf)){ return "Mac"; } if(/X11/.test(pf) || /Linux/.test(pf)){ return "Linux"; } if(/Win/.test(pf)){ if(/Windows NT (\d+\.\d+)/.test(ua)){ switch(RegExp["$1"]){ case "5.0": return "Windows 2000"; case "5.1": return "Windows XP"; case "6.0": return "Windows Vista"; case "6.1": return "Windows 7"; case "6.2": return "Windows 8"; case "6.3": return "Windows 8.1"; case "6.4": case "10.0": return "Windows 10"; } } } } console.log(whichSyStem())//Windows 10
识别移动端
function whichMobile(){ var ua = navigator.userAgent; if(/iPhone OS (\d+_\d+)/.test(ua)){ return 'iPhone' + RegExp.$1.replace("_","."); } if(/iPad.+OS (\d+_\d+)/.test(ua)){ return 'iPad' + RegExp.$1.replace("_",".") } if(/Android (\d+\.\d+)/.test(ua)){ return 'Android' + RegExp["$1"]; } } console.log(whichMobile())//Android 5.1