Detect User Browser and OS using JavaScript

Detect User Browser and OS using JavaScript

Detect User Browser and OS using JavaScript

Introduction

Hello everyone. Welcome to today’s tutorial on Codewithrandom. We’ll learn how to make Detect User Browser and OS which will tell you the current browser on which you are surfing and the Operating system in which you are use in daily. This project will is good for beginners and help them to build their front-end development skills. In Today’s session, We will use HTML, CSS, and JavaScript to complete this Detect User Browser and OS Project.

Detect User Browser & OS WIth Javascript

Project Description

Step 1
The HTML (Hypertext Markup Language) will help us to create the structure for the list with some necessary attributes and elements to make Detect User Browser and OS Project.

Step 2
Then we will use CSS (Cascading Stylesheet) which will help us to style or design the project with suitable padding and alignment in the Detect User Browser and OS Project.

Step 3
At last we will use JS (JavaScript) which will add a logic to make the Detect User Browser and OS Project responsive from the user end.

I hope you have got an idea about the project.

HTML Code for Detect User Browser and OS

First we’ll start with creating the structure of the Detect User Browser and OS project for that as you can see the above code we have used all the necessary elements & attributes to setup the structure. Let us know code the CSS part to add styling and aligned the tags.

Weather App Using Html,Css And JavaScript 

<html lang="en">
  <head>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Detect User Browser and OS</title>
    <!-- Google Font -->
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
      rel="stylesheet"
    />
    <!-- Stylesheet -->
    <link rel="stylesheet" href="style.css" />
  </head>
  <body>
    <div id="container">
      Browser Details: <span id="browser-details"></span><br />
      OS: <span id="os-details"></span>
    </div>
    <!-- Script -->
    <script src="script.js"></script>
  </body>
</html>

CSS Code for Detect User Browser and OS

Second comes the CSS code in which we have styled for the structure we have padded as well as aligned the Detect User Browser and OS project so that it is properly situated and doesn’t get messy with suitable CSS elements. Now we have created the structure using HTML and styled the webpage using CSS its time to add the functionality using JavaScript in this project.

* {
  padding: 0;
  margin: 0;
  box-sizing: border-box;
}
body {
  background-color: #328cf3;
}
#container {
  position: absolute;
  background-color: #ffffff;
  transform: translate(-50%, -50%);
  top: 50%;
  left: 50%;
  width: 90vw;
  max-width: 600px;
  padding: 40px 20px;
  border-radius: 5px;
  font-family: "Poppins", sans-serif;
  font-size: 5vmin;
  color: #051a32;
  font-weight: 600;
  line-height: 1.8em;
  text-align: center;
  box-shadow: 0 20px 50px rgba(5, 26, 50, 0.18);
}
#container span {
  font-weight: 400;
  color: #4b5969;

100+ JavaScript Projects With Source Code ( Beginners to Advanced)

JavaScript Code for Detect User Browser and OS

Last stage of the project the JavaScript in which we have added the logical and coded as per the requirement with some conditions. We have defined the names of the browser and OS and some conditions which will be checked and display on the user screen. Let us see the Final Output of the project Detect User Browser and OS using HTML, CSS & JavaScript (Source Code).

//References
let browserDetailsRef = document.getElementById("browser-details");
let osDetailsRef = document.getElementById("os-details");
var browserList = [
  { name: "Firefox", value: "Firefox" },
  { name: "Opera", value: "OPR" },
  { name: "Edge", value: "Edg" },
  { name: "Chrome", value: "Chrome" },
  { name: "Safari", value: "Safari" },
];
var os = [
  { name: "Android", value: "Android" },
  { name: "iPhone", value: "iPhone" },
  { name: "iPad", value: "Mac" },
  { name: "Macintosh", value: "Mac" },
  { name: "Linux", value: "Linux" },
  { name: "Windows", value: "Win" },
];

let broswerChecker = () => {
  //Useragent contains browser details and OS  details but we need to separate them
  let userDetails = navigator.userAgent;
  for (let i in browserList) {
    //check if the string contains any value from the array
    if (userDetails.includes(browserList[i].value)) {
      //extract browser name and version from the string
      browserDetailsRef.innerHTML = browserList[i].name || "Unknown Browser";
      break;
    }
  }
  for (let i in os) {
    //check if string contains any value from the object
    if (userDetails.includes(os[i].value)) {
      //displau name of OS from the object
      osDetailsRef.innerHTML = os[i].name;
      break;
    }
  }
};

window.onload = broswerChecker();

Output

Live Preview of Detect User Browser and OS using JavaScript

See the Pen
Browser
by Harsh Sawant (@harshh9)
on CodePen.

We have Successfully created our Detect User Browser and OS using HTML, CSS & JavaScript. You can use this project for your personal needs and the respective lines of code are given with the code pen link mentioned above.

If you find out this Blog helpful, then make sure to search codewithrandom on google for Front End Projects with Source codes and make sure to Follow the Code with Random Instagram page.

Code Idea – codingartist

Written By – Harsh Sawant

Code By – @harshh9



Leave a Reply