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";
}
if (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?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…