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

webs95

Aleksandr Semashko

Posted on February 19, 2024

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:

💖 💪 🙅 🚩
webs95
Aleksandr Semashko

Posted on February 19, 2024

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related