JS在web端获取机型片段代码

JS在web中获取机型的片段代码。
通过ua分解出机型(iPhone只能识别出iPhone)、系统版本


        dev_info={
            dev_os: "unknown",
            dev_os_ver: "unknown",
            dev_model: "unknown"
        };
        var u = navigator.userAgent;
        if (u.indexOf('Android') > -1 || u.indexOf('Linux') > -1) {
            dev_info.dev_os = "Android"
            version = u.match(/Android.*?(?=;)/)
            if (version && version.length > 0) {
                dev_info.dev_os_ver = version[0]
            }
            // version = u.match(/(?<=Android )[\d\.]{1,}/g)
            // dev_info.dev_os_ver = version[0]	
            i = u.indexOf('Build/') 
            if(i > -1)
            {
                str_temp = u.substr(0,i)	
                dev_info.dev_model = trim(str_temp.substr(str_temp.lastIndexOf(";")+1))      
            }
        } else 
        if (u.indexOf('iPhone') > -1) {
            dev_info.dev_os = "iOS"
            version = u.match(/iPhone OS .*?(?= )/)
            if (version && version.length > 0) {
                dev_info.dev_os_ver = version[0]
            }
            // version = u.match(/(?<=CPU iPhone OS )[\d\_]{1,}/g)
            // dev_info.dev_os_ver = version[0].replace(/_/g, ".")
            dev_info.dev_model = "iPhone"
        } else if (u.indexOf('iPad') > -1) {
            dev_info.dev_os = "iOS"
            version = u.match(/CPU OS .*?(?= )/)
            if (version && version.length > 0) {
                dev_info.dev_os_ver = version[0]
            }
            // version = u.match(/(?<=CPU OS )[\d\_]{1,}/g)
            // dev_info.dev_os_ver = version[0].replace(/_/g, ".")
            dev_info.dev_model = "iPad"
        } else if (u.indexOf('Windows Phone') > -1) {
            dev_info.dev_os = "WP"
        }
        function trim(str){  
          return str.replace(/^(\s|\xA0)+|(\s|\xA0)+$/g, '');  
        }

你可能感兴趣的:(前端功能实现,javascript,开发语言)