Sarthak Roy
Sarthak Roy's Dev Blog

Sarthak Roy's Dev Blog

Detect Operating System using Javascript

Detect Operating System using Javascript

Sarthak Roy's photo
Sarthak Roy
Oct 2, 2020

2 min read

Subscribe to my newsletter and never miss my upcoming articles

Let's see how we can detect the Operating System on which the website is accessed using Javascript.

Sample Code:

<a>Host:</a>
<a id="detectos"></a>
<script> 
        HTMLDocument.prototype.e = document.getElementById; 
        var exdo = document.e("detectos"); 
        var Name = "Not known"; 
        if (navigator.platform.indexOf('Win32', 'Win64', 'Windows', 'WinCE') != -1) Name =  
          "Windows"; 
        if (navigator.platform.indexOf('Macintosh', 'MacIntel', 'MacPPC', 'Mac68K') != -1) Name =  
          "macOS"; 
        if (navigator.appVersion.indexOf("X11") != -1) Name =  
          "UNIX"; 
        if (navigator.appVersion.indexOf("Linux") != -1) Name =  
          "Linux";
        if (navigator.platform.indexOf('iPhone','iPod') != -1) Name =  
          "iOS";
         if (navigator.platform.indexOf('iPad') != -1) Name =  
          "iPadOS";
        exdo.innerHTML = Name; 
</script>

Output:

Untitled.png

Here, detectos is the id given to the string where the output will be displayed. exdo is simply a variable assigned to the id.

Thanks for the read, Stay Tuned for some more interesting posts.

Share this