Detect the Operating System of User using JavaScript
The task is detect the operating system of User with the help of JavaScript. we’re going to discuss few techniques.
Approach:
- Access the navigator.appVersion or navigator.userAgent property.
- Get the index of the OS using indexOf() method.
- If index is other than -1, then it is the OS, that we are looking for.
Example 1: In this example, navigator.appVersion property is used to get the OS.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Detecting the Operating System of User. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 19px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script> HTMLDocument.prototype.e = document.getElementById; var el_up = document.e("GFG_UP"); var el_down = document.e("GFG_DOWN"); el_up.innerHTML = "Click on the button to get the OS of User's System."; var Name = "Not known"; if (navigator.appVersion.indexOf("Win") != -1) Name = "Windows OS"; if (navigator.appVersion.indexOf("Mac") != -1) Name = "MacOS"; if (navigator.appVersion.indexOf("X11") != -1) Name = "UNIX OS"; if (navigator.appVersion.indexOf("Linux") != -1) Name = "Linux OS"; function GFG_Fun() { el_down.innerHTML = Name; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Example 2: In this example, navigator.userAgent property is used to get the OS.
<!DOCTYPE HTML> <html> <head> <title> JavaScript | Detecting the Operating System of User. </title> </head> <body style="text-align:center;" id="body"> <h1 style="color:green;"> GeeksForGeeks </h1> <p id="GFG_UP" style="font-size: 19px; font-weight: bold;"> </p> <button onclick="GFG_Fun()"> click here </button> <p id="GFG_DOWN" style="color: green; font-size: 24px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); el_up.innerHTML = "Click on the button to get the OS of User's System."; var Name = "Unknown OS"; if (navigator.userAgent.indexOf("Win") != -1) Name = "Windows OS"; if (navigator.userAgent.indexOf("Mac") != -1) Name = "Macintosh"; if (navigator.userAgent.indexOf("Linux") != -1) Name = "Linux OS"; if (navigator.userAgent.indexOf("Android") != -1) Name = "Android OS"; if (navigator.userAgent.indexOf("like Mac") != -1) Name = "iOS"; function GFG_Fun() { el_down.innerHTML = Name; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to detect operating system on the client machine using JavaScript ?
- How to detect the user's device using jQuery ?
- How to detect when user scrolls to the bottom of a div ?
- Detect a device is iOS or not using JavaScript
- How to detect flash is installed or not using JavaScript ?
- How to detect browser or tab closing in JavaScript ?
- How to detect touch screen device using JavaScript?
- JavaScript Course | Interaction With User
- How to check the user is using Internet Explorer in JavaScript?
- How to display hidden element when a user starts typing using JavaScript ?
- How to detect the device is an Android device using JavaScript ?
- How to detect escape key press using jQuery?
- How to detect if a function is called as constructor?
- How to detect a mobile device in jQuery?
- JQuery | Detect a textbox content is changed or not
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.



