How to Detect My Browser Version and Operating System Using JavaScript

How to detect my browser version and operating system using JavaScript?

Detecting browser's details:

var nVer = navigator.appVersion;
var nAgt = navigator.userAgent;
var browserName = navigator.appName;
var fullVersion = ''+parseFloat(navigator.appVersion);
var majorVersion = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;

// In Opera, the true version is after "Opera" or after "Version"
if ((verOffset=nAgt.indexOf("Opera"))!=-1) {
browserName = "Opera";
fullVersion = nAgt.substring(verOffset+6);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {
browserName = "Microsoft Internet Explorer";
fullVersion = nAgt.substring(verOffset+5);
}
// In Chrome, the true version is after "Chrome"
else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {
browserName = "Chrome";
fullVersion = nAgt.substring(verOffset+7);
}
// In Safari, the true version is after "Safari" or after "Version"
else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {
browserName = "Safari";
fullVersion = nAgt.substring(verOffset+7);
if ((verOffset=nAgt.indexOf("Version"))!=-1)
fullVersion = nAgt.substring(verOffset+8);
}
// In Firefox, the true version is after "Firefox"
else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {
browserName = "Firefox";
fullVersion = nAgt.substring(verOffset+8);
}
// 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);
fullVersion = nAgt.substring(verOffset+1);
if (browserName.toLowerCase()==browserName.toUpperCase()) {
browserName = navigator.appName;
}
}
// trim the fullVersion string at semicolon/space if present
if ((ix=fullVersion.indexOf(";"))!=-1)
fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
fullVersion=fullVersion.substring(0,ix);

majorVersion = parseInt(''+fullVersion,10);
if (isNaN(majorVersion)) {
fullVersion = ''+parseFloat(navigator.appVersion);
majorVersion = parseInt(navigator.appVersion,10);
}

document.write(''
+'Browser name = '+browserName+'<br>'
+'Full version = '+fullVersion+'<br>'
+'Major version = '+majorVersion+'<br>'
+'navigator.appName = '+navigator.appName+'<br>'
+'navigator.userAgent = '+navigator.userAgent+'<br>'
)

Source JavaScript: browser name.

See JSFiddle to detect Browser Details.

Detecting OS:

// This script sets OSName variable as follows:
// "Windows" for all versions of Windows
// "MacOS" for all versions of Macintosh OS
// "Linux" for all versions of Linux
// "UNIX" for all other UNIX flavors
// "Unknown OS" indicates failure to detect the OS

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);

source JavaScript: OS detection.

See JSFiddle to detect OS Details.

    var nVer = navigator.appVersion;

var nAgt = navigator.userAgent;

var browserName = navigator.appName;

var fullVersion = ''+parseFloat(navigator.appVersion);

var majorVersion = parseInt(navigator.appVersion,10);

var nameOffset,verOffset,ix;



// In Opera, the true version is after "Opera" or after "Version"

if ((verOffset=nAgt.indexOf("Opera"))!=-1) {

browserName = "Opera";

fullVersion = nAgt.substring(verOffset+6);

if ((verOffset=nAgt.indexOf("Version"))!=-1)

fullVersion = nAgt.substring(verOffset+8);

}

// In MSIE, the true version is after "MSIE" in userAgent

else if ((verOffset=nAgt.indexOf("MSIE"))!=-1) {

browserName = "Microsoft Internet Explorer";

fullVersion = nAgt.substring(verOffset+5);

}

// In Chrome, the true version is after "Chrome"

else if ((verOffset=nAgt.indexOf("Chrome"))!=-1) {

browserName = "Chrome";

fullVersion = nAgt.substring(verOffset+7);

}

// In Safari, the true version is after "Safari" or after "Version"

else if ((verOffset=nAgt.indexOf("Safari"))!=-1) {

browserName = "Safari";

fullVersion = nAgt.substring(verOffset+7);

if ((verOffset=nAgt.indexOf("Version"))!=-1)

fullVersion = nAgt.substring(verOffset+8);

}

// In Firefox, the true version is after "Firefox"

else if ((verOffset=nAgt.indexOf("Firefox"))!=-1) {

browserName = "Firefox";

fullVersion = nAgt.substring(verOffset+8);

}

// 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);

fullVersion = nAgt.substring(verOffset+1);

if (browserName.toLowerCase()==browserName.toUpperCase()) {

browserName = navigator.appName;

}

}

// trim the fullVersion string at semicolon/space if present

if ((ix=fullVersion.indexOf(";"))!=-1)

fullVersion=fullVersion.substring(0,ix);

if ((ix=fullVersion.indexOf(" "))!=-1)

fullVersion=fullVersion.substring(0,ix);



majorVersion = parseInt(''+fullVersion,10);

if (isNaN(majorVersion)) {

fullVersion = ''+parseFloat(navigator.appVersion);

majorVersion = parseInt(navigator.appVersion,10);

}



document.write(''

+'Browser name = '+browserName+'<br>'

+'Full version = '+fullVersion+'<br>'

+'Major version = '+majorVersion+'<br>'

+'navigator.appName = '+navigator.appName+'<br>'

+'navigator.userAgent = '+navigator.userAgent+'<br>'

)

// This script sets OSName variable as follows:

// "Windows" for all versions of Windows

// "MacOS" for all versions of Macintosh OS

// "Linux" for all versions of Linux

// "UNIX" for all other UNIX flavors

// "Unknown OS" indicates failure to detect the OS



var OSName="Unknown OS";

if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";

if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";

if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";

if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";



document.write('Your OS: '+OSName);

How to find the operating system details using JavaScript?

If you list all of window.navigator's properties using

console.log(navigator);

Detect MacOS, iOS, Windows, Android and Linux OS with JS

I learnt a lot about window.navigator object and its properties: platform, appVersion and userAgent. To my mind, it's almost impossible to detect user's OS with 100% sure, but in my case 85%-90% was enough for me.

So, after examining tons of the stackoverflows' answers and some articles, I wrote something like this:

function getOS() {
var userAgent = window.navigator.userAgent,
platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod'],
os = null;

if (macosPlatforms.indexOf(platform) !== -1) {
os = 'Mac OS';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'Windows';
} else if (/Android/.test(userAgent)) {
os = 'Android';
} else if (/Linux/.test(platform)) {
os = 'Linux';
}

return os;
}

alert(getOS());

How can you detect the version of a browser?

You can see what the browser says, and use that information for logging or testing multiple browsers.

navigator.sayswho= (function(){
var ua= navigator.userAgent;
var tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
if(/trident/i.test(M[1])){
tem= /\brv[ :]+(\d+)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
if(M[1]=== 'Chrome'){
tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
}
M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
return M.join(' ');
})();

console.log(navigator.sayswho); // outputs: `Chrome 62`

How to get OS version in PC in Javascript

navigator.platform

this will give you the OS name like
Sample Image

UPDATE

var OSName="Unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";

document.write('Your OS: '+OSName);

On your system, this script yields the following result:
Your OS: Linux
(To get more detailed OS information, your script should perform a more sophisticated analysis of navigator.appVersion or navigator.userAgent, but the idea would be the same.)

How to get the OS name and version with JavaScript?

I'd recommend using platform.js (see demo).

Identify user's browser:

platform.os;
// => OS X 10.15.6 (in my case)

Or parse a userAgent string.

let info = platform.parse("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/14.0.1 Safari/605.1.15");

info.name;
// => Safari

info.version;
// => 14.0.1

info.description;
// => Safari 14.0.1 on OS X 10.15.6


Related Topics



Leave a reply



Submit