I'm looking for a function that returns a boolean value if the user is using a mobile browser or not.
I know that I can use navigator.userAgent and write that function by using regex, but user-agents are too various for different platforms. I doubt that matching all possible devices would be easy, and I think this problem has been solved many times so there should be some kind of complete solution for such a task.
I was looking at this site, but sadly the script is so cryptic that I have no idea how to use it for my purpose, which is to create a function that returns true/false.
Using Regex (from detectmobilebrowsers.com):
Here's a function that uses an insanely long and comprehensive regex which returns a
trueorfalsevalue depending on whether or not the user is browsing with a mobile.For those wishing to include tablets in this test (though arguably, you shouldn't), you can use the following function:
Using navigator.userAgentData
You may also use
navigator.userAgentData.mobile, butuserAgentDatais still experimental, so it is not recommended for use in production.Compatibility chart for userAgentData
The Original Answer
You can do this by simply running through a list of devices and checking if the
useragentmatches anything like so:However since you believe that this method is unreliable, You could assume that any device that had a resolution of 800x600 or less was a mobile device too, narrowing your target even more (although these days many mobile devices have much greater resolutions than this)
i.e
Reference: