5

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

Community
  • 1
  • 1
Anulal S
  • 6,534
  • 5
  • 26
  • 33
  • May I ask which problem you are trying to solve? Depending on the case, there might be other solutions than detecting the device based on the user agent. – Raphael Müller Apr 23 '15 at 06:28
  • Yeah you generally shouldn't have code that depends on the user agent... instead, it's much better to check for the feature that you want in the browser. http://modernizr.com/ is a great free library for just that. – Hamza Kubba Apr 23 '15 at 06:29
  • I am working with cordova and the cordova files should be included only in the device not in desktop browser not even in the emulate option available in the browser. – Anulal S Apr 23 '15 at 06:49

2 Answers2

3

You can try this:

if (navigator.userAgent.match(/android/i)) {
   // it's andorid
} else if (navigator.userAgent.match(/linux/i)) {
   // it's linux
}
jcubic
  • 61,973
  • 54
  • 229
  • 402
2

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.

Community
  • 1
  • 1
UberKaeL
  • 456
  • 5
  • 7