Displaying the OS name/version and the browser name/version on the page?
Is it possible to display the OS name and version on the page with JavaScript?
I am currently using this:
function detectOSAndBrowser() {
var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName = navigator.appName;
var nameOffset,verOffset,ix;
// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
browserName = "Opera";
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
browserName = "Internet Explorer";
}
// In Chrome, the true version is after "Chrome"
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
browserName = "Google Chrome";
}
// In Safari, the true version is after "Safari" or after "Version"
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
browserName = "Safari";
}
// In Firefox, the true version is after "Firefox"
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
browserName = "Mozilla Firefox";
}
// In most other browsers, "name/version" is at the end of userAgent
else if ( (nameOffset=nAgt.lastIndexOf(' ')+1) < (verOffset=nAgt.lastIndexOf('/')) )
{
browserName = nAgt.substring(nameOffset,verOffset);
if (browserName.toLowerCase()==browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
var OSName = "unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) {
OSName="Windows";
}
if (navigator.appVersion.indexOf("Mac")!=-1) {
OSName="Mac OS X";
}
开发者_如何学Pythonif (navigator.appVersion.indexOf("X11")!=-1) {
OSName="UNIX";
}
if (navigator.appVersion.indexOf("Linux")!=-1) {
OSName="Linux";
}
if (OSName!=="unknown OS") {
$('#device').html('You\'re using '+browserName+'<br /><div id="OS">on a '+OSName+' computer.</div>');
}
if (OSName=="unknown OS") {
$('#device').html('We were unable to detect your OS and/or browser.');
}
It works, but I want to display the version of the OS and Browser too. This is how it looks right now when I view it:
How could I make it display the version of the browser and OS also?
It looks like Quirksmode.org is hosting a very well-thought-through script named BrowserDetect
.
It has specialized search strings to extract the real version number for all major browsers.
You're using Chrome 13 on Windows!
To detect the version of the browser, you can use jQuery.browser.version.
精彩评论