Furman
Furman

Reputation: 2425

JavaScript detect mobile device type with UserAgentData API

I'm using UserAgentData browser API to detect whether user has visited my page from mobile device or not. In browsers which supports it, it is quite simple and works well:

const isMobile = window.navigator.userAgentData?.mobile

Problem is that if device is mobile I want to also detect its type, whether it is phone or tablet. I do not want to parse navigator.userAgent string (or use existing libraries which do the same thing). I also do not want to look at device screen width/height and orientation, as as I understand device pixel density can be misleading here. So I'm using high entropy data from userAgentData API to obtain device model:

const detailedUserAgentData = await window.navigator.userAgentData?.getHighEntropyValues(['model']);

Here I have device model name, but still don't know what to do with it - is there any publicly available, open source, regularly updated data base from which I could obtain device type just by its name?

Upvotes: 1

Views: 3658

Answers (0)

Related Questions