javascript获取本机操作系统类型的方法_javascript技巧

废话不多说了,下面通过一段代码示例介绍一下,希望能够给需要的朋友带来或多或少的帮助。

示例代码:

function GetOSInfo()
{
 var _pf=navigator.platform;
 var appVer=navigator.userAgent;
 if(_pf=="Win32" || _pf == "Windows")
 {
  if(appVer.indexOf("WOW64")>-1)
  {
   _bit = "64位";
  }
  else
  {
   _bit = "32位";
  }
  if(appVer.indexOf("Windows NT 6.0") > -1 || appVer.indexOf("Windows Vista") > -1)
  {
   if(_bit=='64位' || appVer.indexOf("Windows Vista") > -1)
   {
    return 'Windows_vista '+_bit;
   }
   else
   {
    return "Unknow1";
   }
  }
  else if(appVer.indexOf("Windows NT 6.1") > -1 || appVer.indexOf("Windows 7") > -1)
  {
   if(_bit=='32位' || appVer.indexOf("Windows 7") > -1)
   {
    return 'Windows_7 '+_bit;
   }
   else
   {
    return "Unknow";
   }
  }
  else
  {
   try
   {
    var _winName = Array('2000','XP','2003');
    var _ntNum = appVer.match(/Windows NT 5.\d/i).toString();
    return 'Windows_' + _winName[_ntNum.replace(/Windows NT 5.(\d)/i,"$1")]+" "+_bit;
   }
   catch(e)
   {
    return 'Windows';
   }
  }
 }
 else if(_pf == "Mac68K" || _pf == "MacPPC" || _pf == "Macintosh")
 {
  return "Mac";
 }
 else if(_pf == "X11")
 {
  return "Unix";
 }
 else if(String(_pf).indexOf("Linux") > -1)
 {
  return "Linux";
 }
 else
 {
  return "Unknow";
 }
}

以上代码很简单的实现了javascript获取本机操作系统类型的方法,一下在通过一段示例代码介绍javascript获取操作系统版本信息

javascript获取操作系统版本信息

示例代码:

var sUserAgent = navigator.userAgent;
var fAppVersion = parseFloat(navigator.appVersion);
//比较浏览器版本大小
function compareVersions(sVersion1,sVersion2){
  var aVersion1 = sVersion1.split(".");
  var aVersion2 = sVersion2.split(".");
  if(aVersion1.length>aVersion2.length){
    for(var i=0;i<aVersion1.length;i++)
      aVersion2.push("0");
  }else if(aVersion1.length<aVersion2.length){
    for(var i=0;i<aVersion2.length;i++)
      aVersion1.push("0");
  }
  for(var i=0;i<aVersion1.length;i++){
    if(aVersion1[i]<aVersion2[i])
      return -1;
    else if(aVersion1[i]>aVersion2[i])
      return 1;
  }
  return 0;
}
//检测是否Opera浏览器
var isOpera = sUserAgent.indexOf("Opera")>-1;
var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false;
if(isOpera){
  var fOperaVersion;
  if(navigator.appName == "Opera"){
    fOperaVersion = fAppVersion;
  } else {
    var reOperaVersion = new RegExp("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
var isKHTML = sUserAgent.indexOf("KHTML")>-1
        || sUserAgent.indexOf("Konqueror")>-1
        || sUserAgent.indexOf("AppleWebKit")>-1;
var isMinSafari1 = isMinSafari1_2 = false;
var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false;
if(isKHTML){
  isSafari = sUserAgent.indexOf("AppleWebKit")>-1;
  isKonq = sUserAgent.indexOf("Konqueror")>-1;
  if(isSafari){
    var reAppleWebKit = new RegExp("AppleWebKit\\/(http://www.cnblogs.com/eoiioe/admin/file://d+(/?:\\.\\d*)?)");
    reAppleWebKit.test(sUserAgent);
    var fAppleWebKitVersion = parseFloat(RegExp["$1"]);
    isMinSafari1 = fAppleWebKitVersion >= 85;
    isMinSafari1_2 = fAppleWebKitVersion >= 124;
  } else if (isKonq) {
    var reKonq = new RegExp("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
var isIE = sUserAgent.indexOf("compatible") > -1
      &&sUserAgent.indexOf("MSIE") > -1
      &&!isOpera;
var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = isMinIE7 = false;
if(isIE){
  var reIE = new RegExp("MSIE (http://www.cnblogs.com/eoiioe/admin/file://d+///d+);");
  reIE.test(sUserAgent);
  var fIEVersion = parseFloat(RegExp["$1"]);
  isMinIE4 = fIEVersion >=4;
  isMinIE5 = fIEVersion >=5;
  isMinIE5_5 = fIEVersion >=5.5;
  isMinIE6 = fIEVersion >=6;
  isMinIE7 = fIEVersion >=7;
}
//检测Firfox
var isFF = sUserAgent.indexOf("Firefox")>-1&&!isKHTML;
var isMinFF1 = isMinFF2 =isMinFF3 = false;
if(isFF){
  var reFF = new RegExp("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
var isMoz = sUserAgent.indexOf("Gecko")>-1&&!isKHTML;
var isMinMoz1 = isMinMoz1_4 = isMinMoz1_5 = isMinMoz1_8 = false;
if(isMoz){
  var reMoz = new RegExp("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;
}
var isNS4 = !isIE&&!isOpera&&!isMoz&&!isKHTML
      &&(sUserAgent.indexOf("Mozilla")==0)
      &&(navigator.appName == "Netscape")
      &&(fAppVersion >= 4.0 && fAppVersion <5.0);
var isMinNS4 = 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;
}
//检测操作系统
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC")
      || (navigator.platform == "Macintosh");
var isUnix = (navigator.platform == "X11" && !isWin && !isMac);
var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = isWin2K3 = isVista = false;
var isMac68K = isMacPPC = false;
var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false;
//Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506)
if(isWin){
  isWin95 = sUserAgent.indexOf("Win95") > -1 || sUserAgent.indexOf("Windows 95") >-1;
  isWin98 = sUserAgent.indexOf("Win98") > -1 || sUserAgent.indexOf("Windows 98") >-1;
  isWinME = sUserAgent.indexOf("Win 9x 4.90") > -1 || sUserAgent.indexOf("Windows ME") >-1;
  isWin2K = sUserAgent.indexOf("Windows NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") >-1;
  isWinXP = sUserAgent.indexOf("Windows NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") >-1;
  isWin2K3 = sUserAgent.indexOf("Windows NT 5.2") > -1 || sUserAgent.indexOf("Windows 2003") >-1;
  //检测Vista系统可能有bug
  isVista = sUserAgent.indexOf("Windows NT 6.0") > -1 || sUserAgent.indexOf("Windows Vista") >-1;
  if(!isWin2K3&&!isVista){
    isWinNT4 = sUserAgent.indexOf("WinNT") > -1
          || sUserAgent.indexOf("Windows NT") >-1
          || sUserAgent.indexOf("WinNT4.0") >-1
          || (sUserAgent.indexOf("Windows NT 4.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){
    var reSunOS = new RegExp("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("Mozilla 1.5 ↑:" + isMinMoz1_5);
document.write("<br/>");
document.write("Mozilla 1.8 ↑:" + isMinMoz1_8);
document.write("<br/>");
document.write("Firfox 1.0 ↑:" + isMinFF1);
document.write("<br/>");
document.write("Firfox 2.0 ↑:" + isMinFF2);
document.write("<br/>");
document.write("Firfox 3.0 ↑:" + isMinFF3);
document.write("<br/>");
document.write("IE 4.0 ↑:" + isMinIE4);
document.write("<br/>");
document.write("IE 5.0 ↑:" + isMinIE5);
document.write("<br/>");
document.write("IE 5.5 ↑:" + isMinIE5_5);
document.write("<br/>");
document.write("IE 6.0 ↑:" + isMinIE6);
document.write("<br/>");
document.write("IE 7.0 ↑:" + isMinIE7);
document.write("<br/>");
document.write("Windows 95:" + isWin95);
document.write("<br/>");
document.write("Windows 98:" + isWin98);
document.write("<br/>");
document.write("Windows ME:" + isWinME);
document.write("<br/>");
document.write("Windows NT:" + isWinNT4);
document.write("<br/>");
document.write("Windows 2000:" + isWin2K);
document.write("<br/>");
document.write("Windows XP:" + isWinXP);
document.write("<br/>");
document.write("Windows 2003:" + isWin2K3);
document.write("<br/>");
document.write("Windows Vista:" + isVista);
document.write("<br/>");

无论是获取本机还是非本机的操作系统版本信息,代码都很简单,希望对大家获多或少的有点帮助。

以上是小编为您精心准备的的内容,在的博客、问答、公众号、人物、课程等栏目也有的相关内容,欢迎继续使用右上角搜索按钮进行搜索javascript获取类型
javascript操作系统
javascript 获取类型、java获取操作系统类型、java 获取系统类型、python 获取系统类型、c 获取系统类型,以便于您获取更多的相关知识。

时间: 2024-10-26 12:58:38

javascript获取本机操作系统类型的方法_javascript技巧的相关文章

javascript限制文本框输入值类型的方法_javascript技巧

本文实例讲述了javascript限制文本框输入值类型的方法.分享给大家供大家参考.具体分析如下: 要求:所有的文本框,只能输入数字和小数点,其他的符号一概不能输入: 重点是我希望在用户输入时就做限制,而不是提交时判断--也就是说如果用户在文本框内输入数字或小数点,可以正常输入:如果输入字母等非数字字符,则文本框会没有任何反应,不会显示输入的字符. <html> <body> <script> var s = "<input type=\"te

详解JavaScript中的4种类型识别方法_javascript技巧

具体内容如下: 1.typeof [输出]首字母小写的字符串形式 [功能] [a]可以识别标准类型(将Null识别为object) [b]不能识别具体的对象类型(Function除外) [实例] console.log(typeof "jerry");//"string" console.log(typeof 12);//"number" console.log(typeof true);//"boolean" console

使用JavaScript获取Request中参数的值方法_javascript技巧

假设现在有一个URL,如下. http://www.jb51.net 如何通过JS访问到id和name里面的值呢,实现我们来分析一下思路. 先获取当前页面的URL,通过window.location.href. 提取该URL?后面的部分,通过slice()方法. 把获取到的Request对象分割成字符串数组,通过split() 方法. 接下来看代码. function getUrlVars() { var vars = [], hash; var hashes = window.location

JavaScript获取当前日期是星期几的方法_javascript技巧

本文实例讲述了JavaScript获取当前日期是星期几的方法.分享给大家供大家参考.具体如下: JavaScript获得当前日期是星期几,主要通过getDay函数获得当前日期是一个星期的第几天 <!DOCTYPE html> <html> <body> <p id="demo"> Click the button to display todays day of the week. </p> <button onclic

JavaScript获取一个范围内日期的方法_javascript技巧

本文实例讲述了JavaScript获取一个范围内日期的方法.分享给大家供大家参考.具体分析如下: 指定开始和结束时间,范围该范围内的所有日期放入数组 Date.prototype.addDays = function(days) { var dat = new Date(this.valueOf()); dat.setDate(dat.getDate() + days); return dat; }; function getDates(startDate, stopDate) { var da

JavaScript获取表单enctype属性的方法_javascript技巧

本文实例讲述了JavaScript获取表单enctype属性的方法.分享给大家供大家参考.具体如下: <!DOCTYPE html> <html> <body> <form id="frm1" enctype="text/plain"> First name: <input type="text" name="fname" value="Donald"&

JavaScript获取两个数组交集的方法_javascript技巧

本文实例讲述了JavaScript获取两个数组交集的方法.分享给大家供大家参考.具体如下: 这里传入的数组必须是已经排过序的 /* finds the intersection of * two arrays in a simple fashion. * * PARAMS * a - first array, must already be sorted * b - second array, must already be sorted * * NOTES * * Should have O(

JavaScript获取数组最小值和最大值的方法_javascript技巧

本文实例讲述了JavaScript获取数组最小值和最大值的方法.分享给大家供大家参考.具体如下: var arr = new Array(); arr[0] = 100; arr[1] = 0; arr[2] = 50; var min = Math.min.apply(null, arr), max = Math.max.apply(null, arr); 以下是补充: var a=[1,2,3,5]; alert(Math.max.apply(null, a));//最大值 alert(Ma

JavaScript获取页面中超链接数量的方法_javascript技巧

本文实例讲述了JavaScript获取页面中超链接数量的方法.分享给大家供大家参考,具体如下: 这里演示JavaScript取得页面的超链接数,感兴趣的朋友可以学习借鉴一下. 运行效果截图如下: 在线演示地址如下: http://demo.jb51.net/js/2015/js-total-link-num-codes/ 具体代码如下: <html> <head> <title>JavaScript取得页面的超链接数</title> <script l