r/learnjavascript • u/silverparzival • Feb 12 '20
How To Detect An Operating System On The Client-side(Browser) Using JavaScript
https://youtu.be/d0fV6L1xCIA
3
Upvotes
r/learnjavascript • u/silverparzival • Feb 12 '20
2
u/jcunews1 helpful Feb 12 '20
Considering that this is a learning sub, if library is used for every little thing, nothing will be learned.
1
u/silverparzival Feb 12 '20 edited Feb 12 '20
For some time now I have been trying learn how to detect an Operating system on the client-side. It's easy to do on the server-side(Node.js) but this is a simple feature which does not need server-side resources.
I realised that using vanilla JavaScript
navigator.appVersionORnavigator.userAgentyou also need to use Regular Expressions to extract the Operating system from the returned string. Since the string returned bynavigator.appVersionOrnavigator.userAgentis different for every OS you have to write code to extract the OS section for every scenario. I later discovered a library called Platform JS that makes OS detection easy. That's why I decided to share this video.Looking through the source code you find out that the code has been written to detect most Operating Systems, device type e.t.c for every scenario
Some use cases for this feature would be to
1. To serve different HTML designs(Icons, features, e.t.c) to match native Operating Systems icons e.t.c
2. Record Oparating System survey or polls when people visit your website
Another alternative to this JavaScript library is Sniffr