DEV Community

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

Aleksandr Semashko on February 19, 2024

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 ...
Collapse
 
aleskuklinek profile image
Aleš Kuklínek

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.

Collapse
 
tnfe profile image
tntweb team

cool

Collapse
 
webs95 profile image
Aleksandr Semashko

thx! :)

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
 
alinnert profile image
Andreas Linnert

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.

Collapse
 
elsyng profile image
Ellis

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?

Collapse
 
syeo66 profile image
Red Ochsenbein (he/him)

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.

Thread Thread
 
elsyng profile image
Ellis

I can and I do ;o)

Collapse
 
sreno77 profile image
Scott Reno

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

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.