DEV Community

Aleksandr Semashko
Aleksandr Semashko

Posted on

Detect MacOS, iOS, Windows, Android and Linux OS with JS

This code snippet detects the user's operating system (OS) and performs a specific action based on their OS. It starts by displaying an element on the webpage with the class "os" where the detected OS will be shown.

The getOS() function determines the user's OS by examining the platform information obtained from the user agent string. It checks for various platform strings associated with popular operating systems such as Mac OS, iOS, Windows, Android, and Linux. Once the OS is determined, it is stored in the os variable.

The detected OS is then displayed on the webpage by assigning it to the innerText property of the selected HTML element. This provides a visual representation of the user's OS.

Additionally, if the detected OS is "Android", the code redirects the user to a specific URL using window.location.replace(). In this case, the URL corresponds to an intent URL for the Instagram app, potentially directing Android users to the app for further interaction. If the OS is not "Android", no redirection occurs.

This code snippet allows for OS-specific handling and customization, enhancing the user experience by providing tailored functionality based on their operating system.

Demo:

Top comments (6)

Collapse
 
tnfe profile image
tntweb team

cool

Collapse
 
webs95 profile image
Aleksandr Semashko

thx! :)

Collapse
 
sreno77 profile image
Scott Reno

This isn't working for me... it says "unknow" for me (using MacOs)

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

Not working on any on my devices. Also, please don't test for OS, test capabilities.

Collapse
 
undeplus profile image
Marc

Not working on m'y Android redme 9

Collapse
 
webs95 profile image
Aleksandr Semashko

Yes, it really is. I've made changes to the code. I'm sorry for this misunderstanding.