DEV Community

Cover image for Detect Operating System using HTML, CSS and JavaScript
Piyush | Coding Torque
Piyush | Coding Torque

Posted on

Detect Operating System using HTML, CSS and JavaScript

Hello Guys! Welcome to Coding Torque. In this blog, I'm going to explain to you how to make a Detect Operating System using HTML, CSS and JavaScript. This will be a step-by-step guide. Let's get started 🚀.

Step 1: Import Fontawesome CDN

Import the font awesome CDN in our HTML <head> tag. fontawesome is a library that is used for icons on a website.

<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
        integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
        crossorigin="anonymous" />
Enter fullscreen mode Exit fullscreen mode

Step 2 : HTML Code

<!DOCTYPE html>
<html lang="en">

<head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">

    <!-- Font Awesome Icons  -->
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.1/css/all.min.css"
        integrity="sha512-+4zCK9k+qNFUR5X+cKL9EIR+ZOhtIloNl9GIKS57V1MyNsYpYcUrUeQc9vNfzsWfV28IaLL3i96P9sdNyeRssA=="
        crossorigin="anonymous" />


    <title>Detect operating system using javascript - @code.scientist x @codingtorque</title>

</head>

<body>
    <div class="center">
        <p>
        <div class="container">
            <h3>Detect Operating System using JavaScript</h3>
            <p>Please click button to display OS</p>

            <button onclick="osname()" class="checkOSBtn">Check OS</button>
            <p id="osname"></p>

        </div>
        </p>
    </div>
</body>

</html>
Enter fullscreen mode Exit fullscreen mode

Output Till Now

Detect Operating System using HTML, CSS and JavaScript

Step 3: CSS Code

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@200;300;400;500;600;700&display=swap');

body {
    display: flex;
    font-family: poppins;
    justify-content: center;
    align-items: center;
    background-color: black;
    color: white;
    height: 600px;
}

.container {
    padding: 40px;
    border: 2px solid deepskyblue;
    border-radius: 10px;
}

#text {
    display: none;
    color: red
}

input {
    font-family: poppins;
    width: 200px;
    height: 30px;
    transition: 0.3s;
    border-radius: 5px;
    padding: 0 5px;
    border: none;
    outline: none;
}

.checkOSBtn {
    padding: 10px 25px;
    border-radius: 10px;
    background-color: deepskyblue;
    color: white;
    font-size: 18px;
    cursor: pointer;
}
Enter fullscreen mode Exit fullscreen mode

Output Till Now

Detect Operating System using HTML, CSS and JavaScript

Step 5: JavaScript Code

HTMLDocument.prototype.e = document.getElementById;
var displayosname = document.e("osname");
var Name = "Not known";
if (navigator.appVersion.indexOf("Win") != -1) Name =
    "Windows Operating System";
if (navigator.appVersion.indexOf("Mac") != -1) Name =
    "Mac Operating System";
if (navigator.appVersion.indexOf("X11") != -1) Name =
    "UNIX Operating System";
if (navigator.appVersion.indexOf("Linux") != -1) Name =
    "Linux Operating System";

function osname() {
    displayosname.innerHTML = Name;
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)