判断操作系统和浏览器的js代码
navigator.userAgent:userAgent 属性是一个只读的字符串,声明了浏览器用于 HTTP 请求的用户代理头的值。
navigator.platform:platform 属性是一个只读的字符串,声明了运行浏览器的操作系统和(或)硬件平台。
判断操作系统类型
win操作系统
1 navigator.platform == "Win32" 2 navigator.platform == "Windows"
win2000操作系统
1 /Windows NT 5.0/i.test(navigator.userAgent)
WinXP操作系统
1 /Windows NT 5.1/i.test(navigator.userAgent)
Win2003操作系统
1 /Windows NT 5.2/i.test(navigator.userAgent)
WinVista操作系统
1 /Windows NT 6.0/i.test(navigator.userAgent)
Win7操作系统
1 /Windows NT 6.1/i.test(navigator.userAgent)
mac操作系统
1 navigator.platform == "Mac68K" 2 navigator.platform == "MacPPC" 3 navigator.platform == "Macintosh" 4 navigator.platform == "MacIntel"
unix操作系统
1 navigator.platform == "X11"
linux操作系统
1 /Linux/i.test(navigator.userAgent)
andorid操作系统
1 if(/Linux/i.test(navigator.userAgent)){ 2 if(/android/i.test(navigator.userAgent.toLowerCase())){ 3 return "android"; 4 } 5 }
判断浏览器类型
ie浏览器:
1 /ie/i.test(navigator.userAgent.toLowerCase());
firefox浏览器
1 /firefox/i.test(navigator.userAgent.toLowerCase());
谷歌浏览器
1 /chrome/i.test(navigator.userAgent.toLowerCase());
opera浏览器
1 /opera/i.test(navigator.userAgent.toLowerCase());
safari浏览器
1 /safari/i.test(navigator.userAgent.toLowerCase());
微信浏览器
1 /micromessenger/i.test(navigator.userAgent.toLowerCase());
本文介绍了一种使用JavaScript来判断用户操作系统的类型及浏览器的方法。通过检查`navigator.userAgent`和`navigator.platform`属性,可以识别出包括Windows、Mac OS、Linux、Android在内的多种操作系统,并能区分IE、Firefox、Chrome等主流浏览器。

1955

被折叠的 条评论
为什么被折叠?



