js获取计算机操作系统版本

如题,想要获取当先计算机的操作系统和版本号的话,可以用如下方法。

首先,创建osversion.js文件,文件里面的代码如下

 1 var osData = [
 2       { name: 'Windows 2000',           group: 'windows_server',      identifier: 'Windows NT 5.0',                     version: '5.0' },
 3       { name: 'Windows XP',             group: 'windows',         identifier: 'Windows NT 5.1',                     version: '5.1' },
 4       { name: 'Windows 2003',           group: 'windows_server',      identifier: 'Windows NT 5.2',                     version: '5.2' },
 5       { name: 'Windows Vista',          group: 'windows',       identifier: 'Windows NT 6.0',                     version: '6.0' },
 6       { name: 'Windows 7',              group: 'windows',       identifier: 'Windows NT 6.1',                     version: '7.0' },
 7       { name: 'Windows 8',              group: 'windows',       identifier: 'Windows NT 6.2',                     version: '8.0' },
 8       { name: 'Windows 8.1',            group: 'windows',       identifier: 'Windows NT 6.3',                     version: '8.1' },
 9       { name: 'Windows 10',             group: 'windows',       identifier: 'Windows NT 10.0',                    version: '10.0' },
10       { name: 'Windows 2008',           group: 'windows_server',    identifier: 'Windows NT 6.0; WOW64',                     version: '6.0' },
11       { name: 'Windows 2008',           group: 'windows_server',    identifier: 'Windows NT 6.1; WOW64',                     version: '6.1' },
12       { name: 'Windows 2012',           group: 'windows_server',    identifier: 'Windows NT 6.3; Win64',                     version: '6.3' },
13       { name: 'Chrome OS',              group: 'windows',      identifier: 'CrOS'                               },
14       { name: 'Mac OS X Capitan',       group: 'mac',        identifier: 'Mac OS X (10([_|\.])11([0-9_\.]*))', versionSeparator: '[_|\.]' },
15       { name: 'Mac OS X Yosemite',      group: 'mac',        identifier: 'Mac OS X (10([_|\.])10([0-9_\.]*))', versionSeparator: '[_|\.]' },
16       { name: 'Mac OS X Mavericks',     group: 'mac',        identifier: 'Mac OS X (10([_|\.])9([0-9_\.]*))',  versionSeparator: '[_|\.]' },
17       { name: 'Mac OS X Mountain Lion', group: 'mac',        identifier: 'Mac OS X (10([_|\.])8([0-9_\.]*))',  versionSeparator: '[_|\.]' },
18       { name: 'Mac OS X Lion',          group: 'mac',        identifier: 'Mac OS X (10([_|\.])7([0-9_\.]*))',  versionSeparator: '[_|\.]' },
19       { name: 'Mac OS X Snow Leopard',  group: 'mac',        identifier: 'Mac OS X (10([_|\.])6([0-9_\.]*))',  versionSeparator: '[_|\.]' },
20       { name: 'Mac OS X Leopard',       group: 'mac',        identifier: 'Mac OS X (10([_|\.])5([0-9_\.]*))',  versionSeparator: '[_|\.]' },
21       { name: 'Mac OS X Tiger',         group: 'mac',        identifier: 'Mac OS X (10([_|\.])4([0-9_\.]*))',  versionSeparator: '[_|\.]' },
22       { name: 'Mac OS X Panther',       group: 'mac',        identifier: 'Mac OS X (10([_|\.])3([0-9_\.]*))',  versionSeparator: '[_|\.]' },
23       { name: 'Mac OS X Jaguar',        group: 'mac',        identifier: 'Mac OS X (10([_|\.])2([0-9_\.]*))',  versionSeparator: '[_|\.]' },
24       { name: 'Mac OS X Puma',          group: 'mac',        identifier: 'Mac OS X (10([_|\.])1([0-9_\.]*))',  versionSeparator: '[_|\.]' },
25       { name: 'Mac OS X Cheetah',       group: 'mac',        identifier: 'Mac OS X (10([_|\.])0([0-9_\.]*))',  versionSeparator: '[_|\.]' },
26       { name: 'Mac OS',                 group: 'mac',        identifier: 'Mac OS'                              },
27       { name: 'Ubuntu',                 group: 'linux_server',         identifier: 'Ubuntu',                             versionIdentifier: 'Ubuntu/([0-9\.]*)' },
28       { name: 'CentOs',                 group: 'linux_server',         identifier: 'CentOs',                             versionIdentifier: 'CentOs/([0-9\.]*)' },
29       { name: 'Debian',                 group: 'linux_server',         identifier: 'Debian'                             },
30       { name: 'Gentoo',                 group: 'linux_server',         identifier: 'Gentoo'                             },
31       { name: '国产系统',               group: 'linux',         identifier: 'Linux'                              }
32 ];
33 
34 var setOsData = function(os) {
35   var userAgent = navigator.userAgent.toLowerCase();
36   // Check browser type
37   for (i in osData) {
38       if (osData.hasOwnProperty(i)){
39         var osRegExp = new RegExp(osData[i].identifier.toLowerCase());
40         var osRegExpResult = osRegExp.exec(userAgent);
41 
42         if (osRegExpResult != null) {
43             os.name = osData[i].name;
44             os.group = osData[i].group;
45             break;
46         }
47       }
48   }
49 
50   return true;
51 };
52 
53 
54 var setOsVersion = function(os,version, separator) {
55   if (separator.substr(0, 1) == '[') {
56       var splitVersion = version.split(new RegExp(separator, 'g'), 2);
57   } else {
58       var splitVersion = version.split(separator, 2);
59   }
60 
61   if (separator != '.') {
62       version = version.replace(new RegExp(separator, 'g'), '.');
63   }
64 
65   os.fullVersion = version;
66 
67   // Major version
68   if (splitVersion[0]) {
69       os.majorVersion = parseInt(splitVersion[0]);
70   }
71 
72   // Minor version
73   if (splitVersion[1]) {
74       os.minorVersion = parseInt(splitVersion[1]);
75   }
76 
77   return true;
78 };

然后在HTML里添加如下代码:

 

<script src="js/osversion.js"></script>
<script>
    var os = {};
    setOsData(os);
    alert(os.name);
</script>

 我们来访问呢一下看下效果:

 弹出来Windows 10,我的计算机的操作系统版本就是这个。

posted @ 2019-07-20 17:07  问北  阅读(4085)  评论(0编辑  收藏  举报