Javascript jQuery - 检测操作系统和操作系统版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8774560/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 07:27:37  来源:igfitidea点击:

jQuery - detecting the operating system and operating system version

javascriptjquerybrowser-detection

提问by ClarkeyBoy

I have been writing a userscript for the past few months, for my company, and have just designed the main site for it with installation instructions (our employees are based all around the world and very few have heard of userscripts, let alone used them, so this frontend is meant to cut down the time I spend supporting the script).

过去几个月我一直在为我的公司编写用户脚本,并且刚刚为它设计了带有安装说明的主站点(我们的员工遍布世界各地,很少有人听说过用户脚本,更不用说使用它们了,所以这个前端是为了减少我花在支持脚本上的时间)。

What I would like to do is, on the installation page, detect which browser and OS / OS version they're using so that I can highlight the most relevant instructions slightly darker than the rest, or simply not display irrelevant sections.

我想要做的是,在安装页面上,检测他们使用的浏览器和操作系统/操作系统版本,以便我可以突出显示最相关的说明,比其他说明稍微暗一些,或者干脆不显示不相关的部分。

For example for IE6 you must use Trixie (I believe) to install userscripts, and this is supported on Win XP only. IE7 is supported on Win XP, IE8 is supported on Win XP & Win 7 and IE9 is supported on Win 7 only. For IE7, 8 & 9 I am advising to use IEPro. The difference between Trixie & IEPro is that Trixie requires a file extension of .user.js which must be saved in C:/Program Files/bhelpuri. IEPro, on the other hand, requires the extension to be .ieuser and saves to a different location. For IE specifically, I would like to detect the version and display only the correct link (either .user.js or .ieuser, depending on what plugin they should be using for their current browser) so that they're taken to the correct version of the file for that browser with the correct save path for that OS / OS version. Is this making any sense so far?

例如,对于 IE6,您必须使用 Trixie(我相信)来安装用户脚本,而这仅在 Win XP 上受支持。Win XP 支持 IE7,Win XP 和 Win 7 支持 IE8,仅 Win 7 支持 IE9。对于 IE7、8 和 9,我建议使用 IEPro。Trixie 和 IEPro 的区别在于 Trixie 需要一个 .user.js 文件扩展名,该文件扩展名必须保存在 C:/Program Files/bhelpuri 中。另一方面,IEPro 要求扩展名为 .ieuser 并保存到不同的位置。特别是对于 IE,我想检测版本并只显示正确的链接(.user.js 或 .ieuser,取决于他们当前浏览器应该使用的插件),以便他们被带到正确的版本该浏览器的文件具有该操作系统/操作系统版本的正确保存路径。

Basically my question is, does anyone know of a way to detect the operating system version? I am currently using http://www.stoimen.com/blog/2009/07/04/jquery-os-detection/but that doesn't give the OS version, only the OS. I have tried looping through all of the variables stored in the navigator object with no success. Any help would be greatly appreciated.

基本上我的问题是,有没有人知道检测操作系统版本的方法?我目前正在使用http://www.stoimen.com/blog/2009/07/04/jquery-os-detection/但这没有提供操作系统版本,只有操作系统。我尝试遍历存储在导航器对象中的所有变量,但没有成功。任何帮助将不胜感激。

Edit: Thanks to Nates answer, I have put the exact code at http://jsfiddle.net/Mu8r5/1/. I hope this helps someone in the future.

编辑:感谢 Nates 的回答,我已经把确切的代码放在http://jsfiddle.net/Mu8r5/1/。我希望这对未来的人有所帮助。

回答by Nate Bolam

Your best bet is to use the navigator.userAgent property. It will give the windows version number. You can see a table of how the Windows version number map to the OS here:

最好的办法是使用 navigator.userAgent 属性。它将给出 Windows 版本号。您可以在此处查看 Windows 版本号如何映射到操作系统的表格:

OSVERSIONINFO

版本信息

Here is some example detection code:

下面是一些示例检测代码:

var os = (function() {
    var ua = navigator.userAgent.toLowerCase();
    return {
        isWin2K: /windows nt 5.0/.test(ua),
        isXP: /windows nt 5.1/.test(ua),
        isVista: /windows nt 6.0/.test(ua),
        isWin7: /windows nt 6.1/.test(ua),
        isWin8: /windows nt 6.2/.test(ua),
        isWin81: /windows nt 6.3/.test(ua)
    };
}());

if(os.isWin7) {
    ...
}

http://jsfiddle.net/45jEc/

http://jsfiddle.net/45jEc/

回答by Aleksandar Vucetic

You can use this great javascript library: http://www.visitorjs.com/detailsIt is open-sourced recently

你可以使用这个很棒的 javascript 库:http: //www.visitorjs.com/details它最近是开源的

Edit: Actually, it is now renamed to session.js http://github.com/codejoust/session.jsand to my knowledge, that is the best you can get.

编辑:实际上,它现在已重命名为 session.js http://github.com/codejoust/session.js,据我所知,这是您能得到的最好的结果。

回答by Luis Perez

The Stack Overflow question Detect exact OS version from browsergoes into some interesting detail about getting the OS version from the User-Agent header which I believe contains the same information that can be accessed from JavaScript.

Stack Overflow 问题 从浏览器检测确切的操作系统版本进入了一些关于从 User-Agent 标头获取操作系统版本的有趣细节,我相信它包含可以从 JavaScript 访问的相同信息。