js中推断浏览器类型

在实际看发展。有时候会遇到在IOS和Android中要用不同的方法处理网页。须要让网页返回当前浏览器的类型。

/**
 * 推断浏览器类型
 */
var Browse = function () {

    //推断是否是苹果系统
    this.browseIos = function () {
        var ua = navigator.userAgent.toLowerCase();
        if (/(iphone|ipad|ipod|ios)/i.test(ua)) {//苹果
            return true;
        }
        return false;
    };

    //推断是否是微信内置浏览器
    this.browseWeiXin = function () {
        var ua = navigator.userAgent.toLowerCase();
        if(/micromessenger/.test(ua)) {
            return true;
        }
        return false;
    };

    //推断是否是安卓
    this.browseAndroid = function () {
        var ua = navigator.userAgent.toLowerCase();
        if (/(android)/i.test(ua)) {//安卓
            return true;
        }
        return false;
    };

    //推断是否是手机端
    this.browseMobile = function () {
        var ua = navigator.userAgent;
        if(ua != null && ua != "") {
            ua = ua.toLowerCase();
        }
        //手机处理
        if(ua.indexOf("android") >= 0 || ua.indexOf("iphone") >= 0 || ua.indexOf("ipod") >= 0 || ua.indexOf("ipad") >= 0 || ua.indexOf("windows phone") >= 0 || ua.indexOf("blackberry") >= 0) {
            return true;
        }
        return false;
    };
};


有时候须要推断是否是在微信中打开。方法类似


function is_weixn(){  
    var ua = navigator.userAgent.toLowerCase();  
    if(ua.match(/MicroMessenger/i)=="micromessenger") {  
        return true;  
    } else {  
        return false;  
    }  
} 







原文地址:https://www.cnblogs.com/yfceshi/p/7228404.html