Javascript detect if on mobile device

Viewed 43

Okay, this has been answered many times before, but it's not working for me. I'm using navigator.userAgent and searching for certain words android|iphone|kindle|ipad. But this doesn't work. When I run on my mobile, userAgent is:

Mozilla/5.0 (Linux: Android 12) AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 Chrome/105.0.5195.79 Mobile DuckDuckGo/5 Safari/537.36

So that works, but when I play on my laptop userAgent is:

Mozilla/5.0 (Linux: Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/105.0.0.0 Mobile Safari/537.36 Edg/105.0.1343.33

So the method I'm using both pick up on "Android" and so both mobile and laptop trigger as mobile.

Is there a different method, or a better way of doing this?

Thanks.

0 Answers
Related