javascript获取本机操作系统类型的方法
废话不多说了,下面通过一段代码示例介绍一下,希望能够给需要的朋友带来或多或少的帮助。
示例代码:
functionGetOSInfo() { var_pf=navigator.platform; varappVer=navigator.userAgent; if(_pf=="Win32"||_pf=="Windows") { if(appVer.indexOf("WOW64")>-1) { _bit="64位"; } else { _bit="32位"; } if(appVer.indexOf("WindowsNT6.0")>-1||appVer.indexOf("WindowsVista")>-1) { if(_bit=='64位'||appVer.indexOf("WindowsVista")>-1) { return'Windows_vista'+_bit; } else { return"Unknow1"; } } elseif(appVer.indexOf("WindowsNT6.1")>-1||appVer.indexOf("Windows7")>-1) { if(_bit=='32位'||appVer.indexOf("Windows7")>-1) { return'Windows_7'+_bit; } else { return"Unknow"; } } else { try { var_winName=Array('2000','XP','2003'); var_ntNum=appVer.match(/WindowsNT5.\d/i).toString(); return'Windows_'+_winName[_ntNum.replace(/WindowsNT5.(\d)/i,"$1")]+""+_bit; } catch(e) { return'Windows'; } } } elseif(_pf=="Mac68K"||_pf=="MacPPC"||_pf=="Macintosh") { return"Mac"; } elseif(_pf=="X11") { return"Unix"; } elseif(String(_pf).indexOf("Linux")>-1) { return"Linux"; } else { return"Unknow"; } }
以上代码很简单的实现了javascript获取本机操作系统类型的方法,一下在通过一段示例代码介绍javascript获取操作系统版本信息
javascript获取操作系统版本信息
示例代码:
varsUserAgent=navigator.userAgent; varfAppVersion=parseFloat(navigator.appVersion); //比较浏览器版本大小 functioncompareVersions(sVersion1,sVersion2){ varaVersion1=sVersion1.split("."); varaVersion2=sVersion2.split("."); if(aVersion1.length>aVersion2.length){ for(vari=0;i<aVersion1.length;i++) aVersion2.push("0"); }elseif(aVersion1.length<aVersion2.length){ for(vari=0;i<aVersion2.length;i++) aVersion1.push("0"); } for(vari=0;i<aVersion1.length;i++){ if(aVersion1[i]<aVersion2[i]) return-1; elseif(aVersion1[i]>aVersion2[i]) return1; } return0; } //检测是否Opera浏览器 varisOpera=sUserAgent.indexOf("Opera")>-1; varisMinOpera4=isMinOpera5=isMinOpera6=isMinOpera7=isMinOpera7_5=false; if(isOpera){ varfOperaVersion; if(navigator.appName=="Opera"){ fOperaVersion=fAppVersion; }else{ varreOperaVersion=newRegExp("Opera(http://www.cnblogs.com/eoiioe/admin/file://d+///d+)"); reOperaVersion.test(sUserAgent); fOperaVersion=parseFloat(RegExp["$1"]); } isMinOpera4=fOperaVersion>=4; isMinOpera5=fOperaVersion>=5; isMinOpera6=fOperaVersion>=6; isMinOpera7=fOperaVersion>=7; isMinOpera7_5=fOperaVersion>=7.5; } //检测是否Safari varisKHTML=sUserAgent.indexOf("KHTML")>-1 ||sUserAgent.indexOf("Konqueror")>-1 ||sUserAgent.indexOf("AppleWebKit")>-1; varisMinSafari1=isMinSafari1_2=false; varisMinKonq2_2=isMinKonq3=isMinKonq3_1=isMinKonq3_2=false; if(isKHTML){ isSafari=sUserAgent.indexOf("AppleWebKit")>-1; isKonq=sUserAgent.indexOf("Konqueror")>-1; if(isSafari){ varreAppleWebKit=newRegExp("AppleWebKit\\/(http://www.cnblogs.com/eoiioe/admin/file://d+(/?:\\.\\d*)?)"); reAppleWebKit.test(sUserAgent); varfAppleWebKitVersion=parseFloat(RegExp["$1"]); isMinSafari1=fAppleWebKitVersion>=85; isMinSafari1_2=fAppleWebKitVersion>=124; }elseif(isKonq){ varreKonq=newRegExp("Konqueror\\/(http://www.cnblogs.com/eoiioe/admin/file://d+(/?:\\.\\d+(?:\\.\\d)?)?)"); reKonq.test(sUserAgent); isMinKonq2_2=compareVersions(RegExp["$1"],"2.2")>=0; isMinKonq3=compareVersions(RegExp["$1"],"3.0")>=0; isMinKonq3_1=compareVersions(RegExp["$1"],"3.1")>=0; isMinKonq3_2=compareVersions(RegExp["$1"],"3.2")>=0; } } //检测IE varisIE=sUserAgent.indexOf("compatible")>-1 &&sUserAgent.indexOf("MSIE")>-1 &&!isOpera; varisMinIE4=isMinIE5=isMinIE5_5=isMinIE6=isMinIE7=false; if(isIE){ varreIE=newRegExp("MSIE(http://www.cnblogs.com/eoiioe/admin/file://d+///d+);"); reIE.test(sUserAgent); varfIEVersion=parseFloat(RegExp["$1"]); isMinIE4=fIEVersion>=4; isMinIE5=fIEVersion>=5; isMinIE5_5=fIEVersion>=5.5; isMinIE6=fIEVersion>=6; isMinIE7=fIEVersion>=7; } //检测Firfox varisFF=sUserAgent.indexOf("Firefox")>-1&&!isKHTML; varisMinFF1=isMinFF2=isMinFF3=false; if(isFF){ varreFF=newRegExp("Firefox\\/(http://www.cnblogs.com/eoiioe/admin/file://d+///d+(?:\\.\\d+\\.\\d+)?)"); reFF.test(sUserAgent); isMinFF1=compareVersions(RegExp["$1"],"1.0")>=0; isMinFF2=compareVersions(RegExp["$1"],"2.0")>=0; isMinFF3=compareVersions(RegExp["$1"],"3.0")>=0; } //检测Mozilla varisMoz=sUserAgent.indexOf("Gecko")>-1&&!isKHTML; varisMinMoz1=isMinMoz1_4=isMinMoz1_5=isMinMoz1_8=false; if(isMoz){ varreMoz=newRegExp("rv:(http://www.cnblogs.com/eoiioe/admin/file://d+///d+(?:\\.\\d+)?)"); reMoz.test(sUserAgent); isMinMoz1=compareVersions(RegExp["$1"],"1.0")>=0; isMinMoz1_4=compareVersions(RegExp["$1"],"1.4")>=0; isMinMoz1_5=compareVersions(RegExp["$1"],"1.5")>=0; isMinMoz1_8=compareVersions(RegExp["$1"],"1.8")>=0; } varisNS4=!isIE&&!isOpera&&!isMoz&&!isKHTML &&(sUserAgent.indexOf("Mozilla")==0) &&(navigator.appName=="Netscape") &&(fAppVersion>=4.0&&fAppVersion<5.0); varisMinNS4=isMinNS4_5=isMinNS4_7=isMinNS4_8=false; if(isNS4){ isMinNS4=true; ifMinNS4_5=fAppVersion>=4.5; ifMinNS4_7=fAppVersion>=4.7; ifMinNS4_8=fAppVersion>=4.8; } //检测操作系统 varisWin=(navigator.platform=="Win32")||(navigator.platform=="Windows"); varisMac=(navigator.platform=="Mac68K")||(navigator.platform=="MacPPC") ||(navigator.platform=="Macintosh"); varisUnix=(navigator.platform=="X11"&&!isWin&&!isMac); varisWin95=isWin98=isWinNT4=isWin2K=isWinME=isWinXP=isWin2K3=isVista=false; varisMac68K=isMacPPC=false; varisSunOS=isMinSunOS4=isMinSunOS5=isMinSunOS5_5=false; //Mozilla/4.0(compatible;MSIE7.0;WindowsNT6.0;SLCC1;.NETCLR2.0.50727;.NETCLR3.0.04506) if(isWin){ isWin95=sUserAgent.indexOf("Win95")>-1||sUserAgent.indexOf("Windows95")>-1; isWin98=sUserAgent.indexOf("Win98")>-1||sUserAgent.indexOf("Windows98")>-1; isWinME=sUserAgent.indexOf("Win9x4.90")>-1||sUserAgent.indexOf("WindowsME")>-1; isWin2K=sUserAgent.indexOf("WindowsNT5.0")>-1||sUserAgent.indexOf("Windows2000")>-1; isWinXP=sUserAgent.indexOf("WindowsNT5.1")>-1||sUserAgent.indexOf("WindowsXP")>-1; isWin2K3=sUserAgent.indexOf("WindowsNT5.2")>-1||sUserAgent.indexOf("Windows2003")>-1; //检测Vista系统可能有bug isVista=sUserAgent.indexOf("WindowsNT6.0")>-1||sUserAgent.indexOf("WindowsVista")>-1; if(!isWin2K3&&!isVista){ isWinNT4=sUserAgent.indexOf("WinNT")>-1 ||sUserAgent.indexOf("WindowsNT")>-1 ||sUserAgent.indexOf("WinNT4.0")>-1 ||(sUserAgent.indexOf("WindowsNT4.0")>-1) &&(!isWinME&&!isWin2K&&!isWinXP); } } if(isMac){ isMac68K=sUserAgent.indexOf("Mac_68000")>-1||sUserAgent.indexOf("68K")>-1; isMacPPC=suserAgent.indexOf("MacPowerPC")>-1||sUserAgent.indexOf("PPC")>-1; } if(isUnix){ isSunOS=sUserAgent.indexOf("SunOS")>-1; if(isSunOS){ varreSunOS=newRegExp("SunOS(http://www.cnblogs.com/eoiioe/admin/file://d+///d+(?:\\.\\d+)?)"); reSunOS.test(sUserAgent); isMinSunOS4=compareVersions(RegExp["$1"],"4.0")>=0; isMinSunOS5=compareVersions(RegExp["$1"],"5.0")>=0; isMinSunOS5_5=compareVersions(RegExp["$1"],"5.5")>=0; } } document.write("<br/>"); document.write("IE浏览器:"+isIE); document.write("<br/>"); document.write("Firfor浏览器:"+isFF); document.write("<br/>"); document.write("Windows操作系统:"+isWin); document.write("<br/>"); document.write("<br/>"); document.write("Mozilla1.5↑:"+isMinMoz1_5); document.write("<br/>"); document.write("Mozilla1.8↑:"+isMinMoz1_8); document.write("<br/>"); document.write("Firfox1.0↑:"+isMinFF1); document.write("<br/>"); document.write("Firfox2.0↑:"+isMinFF2); document.write("<br/>"); document.write("Firfox3.0↑:"+isMinFF3); document.write("<br/>"); document.write("IE4.0↑:"+isMinIE4); document.write("<br/>"); document.write("IE5.0↑:"+isMinIE5); document.write("<br/>"); document.write("IE5.5↑:"+isMinIE5_5); document.write("<br/>"); document.write("IE6.0↑:"+isMinIE6); document.write("<br/>"); document.write("IE7.0↑:"+isMinIE7); document.write("<br/>"); document.write("Windows95:"+isWin95); document.write("<br/>"); document.write("Windows98:"+isWin98); document.write("<br/>"); document.write("WindowsME:"+isWinME); document.write("<br/>"); document.write("WindowsNT:"+isWinNT4); document.write("<br/>"); document.write("Windows2000:"+isWin2K); document.write("<br/>"); document.write("WindowsXP:"+isWinXP); document.write("<br/>"); document.write("Windows2003:"+isWin2K3); document.write("<br/>"); document.write("WindowsVista:"+isVista); document.write("<br/>");
无论是获取本机还是非本机的操作系统版本信息,代码都很简单,希望对大家获多或少的有点帮助。