How to Detect User Operating System in JavaScript

Detect User Operating System in JavaScript

In this article, you will learn how to know the user operating system with the help of JavaScript. Many times the user needs to know the operating system in different projects. 

For example, suppose a user wants to download an application from your website. Many applications are different for different operating systems. So which version to download depends on the user's OS.

This is a very simple project which can easily detect the user's operating system by JavaScript. But for this, you need to know some JavaScript.

Detect User Operating System in JavaScript

Its design is very simple. A simple box has been created on the webpage. The name of the OS that will open the webpage by the operating system can be seen here.

See the Pen Untitled by Shantanu Jana (@shantanu-jana) on CodePen.


1. A place to see the results of OS

First, a box is created using the button tag. Although you can create boxes with any tag instead of this button.


<button id="download"></button>

Now we have to design the webpage with some CSS. Here I have used black as the background color of the webpage.


* {
margin: 0;
padding: 0;
border: none;
box-sizing: border-box;
-webkit-tap-highlight-color: transparent;
}

html,
body {
height: 100%;
}

body {
display: flex;
align-items: center;
justify-content: center;
background: #0d191f;
}


A place to see the results of OS

Now the button has been designed. The background color is white and the text color is blue.


button#download {
font-family: "Open Sans", sans-serif;
user-select: none;
outline: none;
color: #0449a4;
background: #ffffff;
padding: 2rem 4rem;
border-radius: 0.2rem;
font-size: 30px;
transition: opacity 0.2s ease;
}


button has been designed

2. Add the hover effect to the button

Now I have added hover and disabled effects in that button. The disabled effect can be seen when the project cannot detect your operating system.


button#download:hover {
cursor: pointer;
opacity: 0.8;
}

button#download:disabled {
cursor: no-drop;
opacity: 0.5;
background: #f8f8f8;
color: #121212;
}

3. Collect user's Operating System data

Now I have used some JavaScript. But you will not have any problem because some simple and small amount of JavaScript has been used here.


//Constant of button's id
const button = document.getElementById("download");

//Navigator Platform string provides a very simple outline of the type of platform your web browser
const platform = navigator.platform;
let buttonText = "You are using ";
//platform.startsWith lets you inspect what operating system the code is running on
if (platform.startsWith("Win")) {
  buttonText += "Windows";
} else if (platform.startsWith("Mac")) {
  buttonText += "Mac";
} else if (platform.startsWith("X11")) {
  buttonText += "UNIX";
}  else if (platform.startsWith("Linux")) {
  buttonText += "Linux";
}
else {
  buttonText = "Sorry, I don't know!";
  button.setAttribute("disabled", "true");
}
//The textContent property sets or returns the text content of the specified node
button.textContent = buttonText;

User Operating System in JavaScript

Hopefully, you have learned from this tutorial how to detect User Operating systems by JavaScript. Below you will find a button to download the source code.








Comments

Popular posts from this blog

Simple Day Night Toggle Button Using HTML and CSS

Splash Image Mask Using HTML and CSS (Free Code)

Simple RGB Color Generator Using JavaScript & CSS