관리 메뉴

세줄코딩

디바이스 정보 알아내기. 본문

Programming language/javascript

디바이스 정보 알아내기.

개발 hello-world 2019.06.08 16:23

디바이스 정보 알아내기.

정리가 필요할 듯 ~

//사용법
getDeviceInfo( userAgentCheck() );


function userAgentCheck() {
        var isIE, isIEver, ieFlag, isEDGE,
            isChrome, chromeVer,
            isFireFox, fireFoxVer,
            isSafari, safariVer,
            isOpera, operaVer,
            isMac,
            isIPhone,
            isIPad,
            isIOSver,
            isAndroid,
            androidVer,
            androidMobile,
            androidTablet,
            browserInfo,
            userAgent = window.navigator.userAgent.toLowerCase();
        //console.log( userAgent )
        // 브라우저 설정.
        if (userAgent.indexOf("msie") != -1) {
            isIE = true;
            userAgent.match(/msie (\d+\.\d)/);
            isIEver = parseFloat(RegExp.$1);
        } else if (window.navigator.appName === 'Netscape' && userAgent.indexOf('trident') > -1) {
            isIE = true;
            ieFlag = 'trident/.*rv:';
            var versionSort = '([0-9]{1,})(\\.{0,}[0-9]{0,1})';
            var numCheckReg = new RegExp(ieFlag + versionSort);
            if (numCheckReg.exec(userAgent) != null) {
                isIEver = parseFloat(RegExp.$1);
            } else {
                isIEver = 11;
            }
        } else if (userAgent.indexOf('edge') > -1) {
            isEDGE = true;
            ieFlag = 'edge';
            var matches = userAgent.match(/edge\/(\d+\.\d)/);
            isIEver = ( matches !== null ) ? matches[1] : 0;
        } else if (userAgent.indexOf("chrome") !== -1) {
            isChrome = true;
            userAgent.match(/chrome[\/ ]?(\d+\.\d+)/);
            chromeVer = parseFloat(RegExp.$1);
        } else if (userAgent.indexOf("firefox") !== -1) {
            isFireFox = true;
            userAgent.match(/firefox[\/ ]?(\d+\.\d+)/);
            fireFoxVer = parseFloat(RegExp.$1);

        } else if (userAgent.indexOf("opera") != -1) {
            isOpera = true;
            userAgent.match(/opera[\/ ]?(\d+\.\d+)/);
            operaVer = parseFloat(RegExp.$1);
        } else if (userAgent.indexOf("safari") !== -1) {
            isSafari = true;
            userAgent.match(/version[\/ ]?(\d+\.\d+)/);
            safariVer = parseFloat(RegExp.$1);
        }

        if (userAgent.indexOf("iphone") != -1) {
            isIPhone = true;
            userAgent.match(/iphone os (\d+)_(\d+)/);
            isIOSver = RegExp.$1 * 1 + RegExp.$2 * 0.1;
        } else if (userAgent.indexOf("ipad") != -1) {
            isIPad = true;
            userAgent.match(/cpu os (\d+)_(\d+)/);
            isIOSver = RegExp.$1 * 1 + RegExp.$2 * 0.1;
        } else if (userAgent.indexOf("android") != -1) {
            isAndroid = true;
            userAgent.match(/android (\d+\.\d)/);
            androidVer = parseFloat(RegExp.$1);
            if (userAgent.indexOf('mobile') != -1) {
                androidMobile = true;
            } else {
                androidTablet = true;
            }
        }

        if (userAgent.indexOf('mac os') !== -1) {
            isMac = true;
        }


        browserInfo = [
            {has: isIE, type: 'ie', version: isIEver},
            {has: isEDGE, type: 'edge', version: isIEver},
            {has: isChrome, type: 'chrome', version: chromeVer},
            {has: isFireFox, type: 'firefox', version: fireFoxVer},
            {has: isSafari, type: 'safari', version: safariVer},
            {has: isOpera, type: 'opera', version: operaVer},
            {has: isIPhone, type: 'iphone', version: isIOSver},
            {has: isIPad, type: 'ipad', version: isIOSver},
            {has: isAndroid, type: 'android', version: androidVer},
            {has: androidMobile, type: 'android-mobile', version: androidVer},
            {has: androidTablet, type: 'android-tablet', version: androidVer}
        ];

        var results = [];

        for (var i = 0, len = browserInfo.length; i < len; i++) {
            if (browserInfo[i].has === true) {
                //console.log(_browserInfo[i].type, _browserInfo[i].item, _browserInfo[i].version )
                results.push({
                    type: browserInfo[i].type,
                    version: browserInfo[i].version
                });
            }
        }

        return results;
    }

    function getDeviceInfo( data ) {
        var reg = /iphone|ipad|android|android-mobile|android-tablet/g;
        for (var i = 0; i < data.length; i++) {
            if (reg.test(data[i].type)) {
                // console.log( 'results[i].type='+data[i].type )
                return data[i].type;
            }
        }
        return -1;
    }
0 Comments
댓글쓰기 폼