1. 程式人生 > 其它 >js -- 客戶端判斷 瀏覽器判斷 微信判斷 ios android web判斷

js -- 客戶端判斷 瀏覽器判斷 微信判斷 ios android web判斷

瀏覽器對於我們來說,可能是最熟悉的工具了。熟知的瀏覽器Firefox、Opera、Safari、IE、Chrome以外,據說世界上還有近百種瀏覽器。通常在開發的時候要做到相容各種瀏覽器,因此提煉出判斷瀏覽器型別及系統是很重要的。

//各主流瀏覽器

// 各主流瀏覽器
function getBrowser() {
    var u = navigator.userAgent;
 
    var bws = [{
        name: 'sgssapp',
        it: /sogousearch/i.test(u)
    }, {
        name: 'wechat',
        it: 
/MicroMessenger/i.test(u) }, { name: 'weibo', it: !!u.match(/Weibo/i) }, { name: 'uc', it: !!u.match(/UCBrowser/i) || u.indexOf(' UBrowser') > -1 }, { name: 'sogou', it: u.indexOf('MetaSr') > -1 || u.indexOf('Sogou') > -1 }, { name:
'xiaomi', it: u.indexOf('MiuiBrowser') > -1 }, { name: 'baidu', it: u.indexOf('Baidu') > -1 || u.indexOf('BIDUBrowser') > -1 }, { name: '360', it: u.indexOf('360EE') > -1 || u.indexOf('360SE') > -1 }, { name: '2345', it: u.indexOf(
'2345Explorer') > -1 }, { name: 'edge', it: u.indexOf('Edge') > -1 }, { name: 'ie11', it: u.indexOf('Trident') > -1 && u.indexOf('rv:11.0') > -1 }, { name: 'ie', it: u.indexOf('compatible') > -1 && u.indexOf('MSIE') > -1 }, { name: 'firefox', it: u.indexOf('Firefox') > -1 }, { name: 'safari', it: u.indexOf('Safari') > -1 && u.indexOf('Chrome') === -1 }, { name: 'qqbrowser', it: u.indexOf('MQQBrowser') > -1 && u.indexOf(' QQ') === -1 }, { name: 'qq', it: u.indexOf('QQ') > -1 }, { name: 'chrome', it: u.indexOf('Chrome') > -1 || u.indexOf('CriOS') > -1 }, { name: 'opera', it: u.indexOf('Opera') > -1 || u.indexOf('OPR') > -1 }]; for (var i = 0; i < bws.length; i++) { if (bws[i].it) { return bws[i].name; } } return 'other'; }
瀏覽器區分

//系統區分

// 系統區分
function getOS() {
    var u = navigator.userAgent;
    if (!!u.match(/compatible/i) || u.match(/Windows/i)) {
        return 'windows';
    } else if (!!u.match(/Macintosh/i) || u.match(/MacIntel/i)) {
        return 'macOS';
    } else if (!!u.match(/iphone/i) || u.match(/Ipad/i)) {
        return 'ios';
    } else if (!!u.match(/android/i)) {
        return 'android';
    } else {
        return 'other';
    }
}
系統區分

參考:

js如何判斷使用者是否用微信瀏覽器

https://www.jb51.net/article/50708.htm

navigator.userAgent獲取瀏覽器資訊(型別及系統)

https://blog.csdn.net/banana960531/article/details/86572475

  1. // 各主流瀏覽器

本文僅提供參考,是本人閒時所寫筆記,如有錯誤,還請賜教,作者:阿蒙不萌,大家可以隨意轉載