Program Club

Javascript로 브라우저에서 Android 버전을 선택하십시오.

proclub 2020. 12. 2. 22:08
반응형

Javascript로 브라우저에서 Android 버전을 선택하십시오.


웹 앱을 빌드 중이며 버전 3.0 미만의 Android 장치에서 전환 효과를 비활성화하고 싶습니다.

어쨌든 브라우저에서 Javascript로 Android 버전 번호를 선택할 수 있습니까? 그렇다면 어떻게?


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

아래 코드를 사용하여 Android의 2 자리 버전을 얻으십시오.

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

예를 들면

Mozilla / 5.0 (Linux; U; Android 2.2.1; fr-ch; A43 Build / FROYO) AppleWebKit / 533.1 (Gecko와 같은 KHTML) 버전 /4.0 Mobile Safari / 533.1

Android 버전 = 2.2를 반환합니다.


사용자 에이전트 문자열 ( https://developer.mozilla.org/en/DOM/window.navigator.userAgent)을 볼 수 있습니다 window.navigator.userAgent.

실제로 감지하려는 것이 특정 기능을 지원하는 브라우저 버전이 있는지 여부 인 경우 브라우저 버전 감지 대신 기능 감지를 사용하는 것이 거의 항상 좋습니다. modernizr는 그대로 사용하거나 특정 부분을 빌리거나 일반적인 기술이 어떻게 작동하는지 배울 수있는 기능 감지를위한 거대한 코드 기반입니다.

Google에서 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

정규식 /Android\s+([\d\.]+)/on window.navigator.userAgent은 Android 버전 번호를 선택합니다.


담당자가 충분하지 않아 댓글을 달 수 없습니다 ... neiker의 코드를 다음과 같이 변경해야한다고 추가하고 싶었습니다.

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

Galaxy S3가 사용자 에이전트에서 Android 대신 "android"를 반환했기 때문에 대소 문자를 구분하지 않습니다.


Motorola의 플레이어 사용자 에이전트는 다음을 가질 수 있습니다.

Linux;Android ; Release/4.1.2

따라서 다음 정규식을 사용하기 시작해야했습니다.

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

이 코드는 useragent에서 Android의 전체 버전을 확인합니다.

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;
    }
}

이 기능을 만들었고 다른 브라우저와 장치에서 Android 2.6.3, 4.2.2., 6.0.1에서 매우 잘 작동했습니다. 나는 그것이 대안이 될 수 있다고 생각합니다.

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;
}

이것은 아주 간단합니다. 먼저 "android"라는 단어를 검색합니다. 찾을 수없는 경우 0을 반환합니다 (Android 사용자 에이전트가 아님). 그런 다음 첫 번째 ';'을 검색합니다. 버전의 끝을 표시하는 "android"위치 뒤에 있습니다. 이러한 위치가 확보되면 '슬라이스'는 버전 번호를 절연 ( '+8'은 최종 결과의 "android"라는 단어를 제거함)을 반환합니다.

누군가 결함을 발견하면 공유하는 것이 좋습니다.


Windows 모바일은 사용자 에이전트에 Android를 포함 할 수 있으므로 먼저 장치가 Android인지 아닌지 확인하는 것이 좋습니다.

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 Detecting iOS / Android Operating system


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]) );

참고URL : https://stackoverflow.com/questions/7184573/pick-up-the-android-version-in-the-browser-by-javascript

반응형