admin管理员组

文章数量:1323157

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

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

Share edited May 23, 2017 at 11:45 CommunityBot 11 silver badge asked Apr 23, 2015 at 6:24 Anulal SAnulal S 6,6255 gold badges28 silver badges34 bronze badges 3
  • 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 Commented Apr 23, 2015 at 6: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. modernizr. is a great free library for just that. – Hamza Kubba Commented Apr 23, 2015 at 6: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 Commented Apr 23, 2015 at 6:49
Add a ment  | 

2 Answers 2

Reset to default 3

You can try this:

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

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

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

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

本文标签: JavascriptIdentify whether it is a desktop Linux or AndroidStack Overflow