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


Posted in Javascript onAugust 13, 2015

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

示例代码:

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 相关文章推荐
图片onload事件触发问题解决方法
Jul 31 Javascript
javascript 常用功能总结
Mar 18 Javascript
Microsfot .NET Framework4.0框架 安装失败的解决方法
Aug 14 Javascript
jQuery中DOM树操作之使用反向插入方法实例分析
Jan 23 Javascript
jQuery实现textarea自动增长宽高的方法
Dec 18 Javascript
jquery实现左右无缝轮播图
Jul 31 Javascript
vue如何引用其他组件(css和js)
Apr 13 Javascript
关于页面刷新vuex数据消失问题解决方案
Jul 03 Javascript
JS常见构造模式实例对比分析
Aug 27 Javascript
vxe-table vue table 表格组件功能
May 26 Javascript
详解vue-cli3多页应用改造
Jun 04 Javascript
javascript设计模式之装饰者模式
Jan 30 Javascript
javascript中offset、client、scroll的属性总结
Aug 13 #Javascript
用JavaScript实现PHP的urlencode与urldecode函数
Aug 13 #Javascript
asp.net中oracle 存储过程(图文)
Aug 12 #Javascript
JavaScript的jQuery库插件的简要开发指南
Aug 12 #Javascript
JavaScript中的call方法和apply方法使用对比
Aug 12 #Javascript
详细解读JavaScript的跨浏览器事件处理
Aug 12 #Javascript
基于jQuery实现动态数字展示效果
Aug 12 #Javascript
You might like
PHP-redis中文文档介绍
2013/02/07 PHP
PHP实现图片裁剪、添加水印效果代码
2014/10/01 PHP
php开发中的页面跳转方法总结
2015/04/26 PHP
php结合md5实现的加密解密方法
2016/01/25 PHP
微信支付开发发货通知实例
2016/07/12 PHP
PHP文件及文件夹操作之创建、删除、移动、复制
2016/07/13 PHP
session 加入redis的实现代码
2016/07/15 PHP
php使用高斯算法实现图片的模糊处理功能示例
2016/11/11 PHP
laravel实现分页样式替换示例代码(增加首、尾页)
2017/09/22 PHP
用函数式编程技术编写优美的 JavaScript
2006/11/25 Javascript
[HTML/CSS/Javascript]WWTJS
2007/09/25 Javascript
Javascript 对象的解释
2008/11/24 Javascript
javascript学习笔记(八) js内置对象
2012/06/19 Javascript
浅谈js数组和splice的用法
2016/12/04 Javascript
jQuery Password Validation密码验证
2016/12/30 Javascript
JS排序之冒泡排序详解
2017/04/08 Javascript
bootstrap表单示例代码分享
2017/05/18 Javascript
JS实现多选框的操作
2020/06/24 Javascript
基于hashlib模块--加密(详解)
2017/06/21 Python
详解Golang 与python中的字符串反转
2017/07/21 Python
python 显示数组全部元素的方法
2018/04/19 Python
Python读写/追加excel文件Demo分享
2018/05/03 Python
详解Python如何生成词云的方法
2018/06/01 Python
用Pycharm实现鼠标滚轮控制字体大小的方法
2019/01/15 Python
Python数据类型之Dict字典实例详解
2019/05/07 Python
利用python-pypcap抓取带VLAN标签的数据包方法
2019/07/23 Python
深入了解Python在HDA中的应用
2019/09/05 Python
如何使用Python抓取网页tag操作
2020/02/14 Python
通过代码实例了解Python异常本质
2020/09/16 Python
Django多数据库联用实现方法解析
2020/11/12 Python
国贸专业的职业规划范文
2014/01/23 职场文书
项目合作协议书
2014/04/16 职场文书
2014年部门工作总结
2014/11/12 职场文书
优秀教师工作总结2015
2015/07/22 职场文书
信息技术研修心得体会
2016/01/08 职场文书
MySQL中存储时间的最佳实践指南
2021/07/01 MySQL