Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
258 views
in Technique[技术] by (71.8m points)

javascript - jQuery - detecting the operating system and operating system version

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?

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.

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...