Pick up the Android version in the browser by Javascript

JavascriptAndroidBrowserAndroid Api-Levels

Javascript Problem Overview


I'm building a web app and wanting to disable transitions effects on Android devices under version 3.0.

Is there anyway to pick up the Android version number by Javascript in the browser? If so how?

Javascript Solutions


Solution 1 - Javascript

function getAndroidVersion(ua) {
    ua = (ua || navigator.userAgent).toLowerCase(); 
    var match = ua.match(/android\s([0-9\.]*)/i);
    return match ? match[1] : undefined;
};

getAndroidVersion(); //"4.2.1"
parseInt(getAndroidVersion(), 10); //4
parseFloat(getAndroidVersion()); //4.2

Solution 2 - Javascript

Use below code to get 2 digit version of Android

var ua = navigator.userAgent;
if( ua.indexOf("Android") >= 0 )
{
  var androidversion = parseFloat(ua.slice(ua.indexOf("Android")+8)); 
  if (androidversion < 2.3)
  {
      // do whatever
  }
}

For example

>Mozilla/5.0 (Linux; U; Android 2.2.1; fr-ch; A43 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

will return Android Version = 2.2

Solution 3 - Javascript

I can't comment because I don't have enough rep... Just wanted to add that I had to change neiker's code to

var match = ua.match(/Android\s([0-9\.]*)/i);

to make it case insensitive because the Galaxy S3 was returning "android" instead of Android in its user agent

Solution 4 - Javascript

You can look at the user agent string - window.navigator.userAgent described here: https://developer.mozilla.org/en/DOM/window.navigator.userAgent

If what you're really trying to detect is whether you have a version of the browser that supports a particular feature, then it's nearly always better to use feature detection instead of browser version detection. modernizr is a huge base of code for feature detection that you can either use as is or borrow one particular piece from or just learn how the general technique works.

When I Google, I see user agent strings like this for Android:

Mozilla/5.0 (Linux; U; Android 2.2.1; fr-ch; A43 Build/FROYO) AppleWebKit/533.1 (KHTML, like Gecko) Version/4.0 Mobile Safari/533.1

A regex of /Android\s+([\d\.]+)/ on window.navigator.userAgent will pick up the Android version number.

Solution 5 - Javascript

Motorola's player user agents can have the following:

Linux;Android ; Release/4.1.2

So, I've had to start using the the following regex:

[a|A]ndroid[^\d]*([\d[_|.]]+\d)

Solution 6 - Javascript

This code checks the full version of Android from the useragent.

var test = LowerThanAndroidVersion('4.4');
if (test) {
    alert('lower than android 4.4')
} else if (test == undefined) {
    alert('no android')
} else {
    alert('android 4.4 or higher');
}    

function LowerThanAndroidVersion(testversion) {
    //var useragent = 'Mozilla/5.0 (Linux; U; Android 4.3.1; en-gb; GT-I9300 Build/IMM76D) AppleWebKit/534.30 (KHTML, like Gecko) Version/4.0 Mobile Safari/534.30';
    var useragent = navigator.userAgent;
    var androidpoint = useragent.indexOf('Android');
    if (androidpoint >= 0) {
        var rest = useragent.substring(androidpoint + 8, useragent.length);
        var version = rest.substring(0, rest.indexOf(';'));
        return (version < testversion) ? true : false;
    }
}

Solution 7 - Javascript

I made this function and worked very well in Android 2.6.3, 4.2.2., 6.0.1 in different browsers and devices. I think that can be an alternative.

function check_android_version()
{
    var ua = navigator.userAgent; 
    ua = ua.toLowerCase();

    var and_pos = ua.search("android");
    if(and_pos == -1)
	    return 0; //not android
	
    var pv_pos = ua.indexOf(";", and_pos);
    var versao = ua.slice(and_pos+8, pv_pos);

    return versao;
}

This is quite simple, first, search for the word "android". If not found, returns 0 (is not an Android user agent). Then search for the first ';' after the "android" position that marks the end of the version. Once these positions are gotten, the 'slice' insulates the version numbers (that '+8' removes the word "android" of the final result) an then returns.

If someone find any flaw, would be nice to share.

Solution 8 - Javascript

It's better to check first if device is android or not as Windows mobile can have Android in its user agent.

Example: Mozilla/5.0 (Windows Phone 10.0; Android 4.2.1; ; ) AppleWebKit/ (KHTML, like Gecko) Chrome/ Mobile Safari/ Edge/.

Here is way to check it https://stackoverflow.com/questions/21741841/detecting-ios-android-operating-system

Solution 9 - Javascript

1- Windows phone introduced a fake "android" user agent, so this deal with it
2- As @andy pointed out, there are some models with this format: Android/#.##
3- You can use the flag /i for case-insensitive
4- This code gives you back NaN when its not android and the version as a float when it is.
5- If you find more fake android phones you only need to add the string like this: (?:windows phone|fake phone|android...........

alert( parseFloat(navigator.userAgent.match(/.*?(?:windows phone|android\D+([0-9\.]*))|()/i)[1]) );

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDaniel RyanView Question on Stackoverflow
Solution 1 - JavascriptneikerView Answer on Stackoverflow
Solution 2 - JavascriptGohel KiranView Answer on Stackoverflow
Solution 3 - Javascriptjordan314View Answer on Stackoverflow
Solution 4 - Javascriptjfriend00View Answer on Stackoverflow
Solution 5 - JavascriptandyView Answer on Stackoverflow
Solution 6 - JavascriptMdRView Answer on Stackoverflow
Solution 7 - JavascriptNey CândidoView Answer on Stackoverflow
Solution 8 - JavascriptSanchay KumarView Answer on Stackoverflow
Solution 9 - JavascriptYOGOView Answer on Stackoverflow