Search code examples
javascripthtmloperating-systemuser-agentnavigator

How to get the OS name and version with JavaScript?


I am creating an object to make it easier to access browser/system information by calling a function. One of those functions accesses the operating system name and version and returns a value.

const Sys = {
  // retrieves the operating system
  OS: function () {
    // function body
  }
  // other functions...
}

I don't know how to go about getting the information I need. I did find a similar question on Stack Overflow, but it didn't get the correct information. For example, if I am running on a Windows 10 Pro 32-bit, I want the output to be "Windows 10 Pro 32-bit". I'm guessing I have to use the navigator object, but other than that I really don't know anything else. Can anyone help with this?


Solution

  • 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