Anulal S
Anulal S

Reputation: 6625

Javascript : Identify whether it is a desktop Linux or Android

Unable to identify whether it is a linux desktop machine or an android device using navigator.userAgent or navigator.platform as some android device's have the string linux in both. Details follows

Device                           OS               navigator.platform  
--------------------------------------------------------------------
Samsung Galaxy S3                Android 4.3      Linux armv7l
HTC One                          Android 4.4.2    Linux armv7l
Sony Xperia Z                    Android 4.2.2    Linux armv7l
Motorola Moto G                  Android 4.4.2    Linux armv7l
Samsung Galaxy Tab 3             Android 4.2.2    Linux i686
Nexus 10                         Android 4.4.2    Linux armv7l
Lenovo Yoga                      Android 4.2.2    Linux armv7l

navigator.userAgent

Mozilla/5.0 (Linux; U; Android 2.2; en-us; SCH-I800 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

Even I tried with touch events, but linux desktop can have touch or it can emulate touch. please help

Update: The solution should detect Linux even if Desktop browser's emulate device is active. View Details

Upvotes: 5

Views: 7975

Answers (2)

jcubic
jcubic

Reputation: 66560

You can try this:

if (navigator.userAgent.match(/android/i)) {
   // it's andorid
} else if (navigator.userAgent.match(/linux/i)) {
   // it's linux
}

Upvotes: 3

UberKaeL
UberKaeL

Reputation: 458

Browser identification based on detecting the user agent string is unreliable and is not recommended

Nowadays browser detection is not a good practice, instead people use feature detection based on javascript or @media queries.

I recommend to read this answer, maybe you can see the problem from another point of view.

Upvotes: 2

Related Questions