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 (11)
cool
thx! :)
Not working on any on my devices. Also, please don't test for OS, test capabilities.
A very legit use-case for this are keyboard shortcuts. E.g. on Macs, iPhone and iPad bind "meta + s", but "control + s" everywhere else. Also for displaying that shortcut: "⌘ + S" vs. "Ctrl + S". I couldn't find an alternative for that. Even the experimental
navigator.keyboard.getLayoutMap()
doesn't seem to give the information needed for that.If a font is displayed differently per OS (different height or space values), if I know which OS it is, then I can compensate. Do you know a "capability" for this?
Sorry, can't think of any case where this is needed. I have the feeling you're trying to control things you can't control.
I can and I do ;o)
This isn't working for me... it says "unknow" for me (using MacOs)
Not working on m'y Android redme 9
Yes, it really is. I've made changes to the code. I'm sorry for this misunderstanding.
Thank you, i will use your script to detect platform and show games based on user browser. Thank you lot. You safe me half hour of my life.