JS判斷瀏覽器

??判斷是不是移動(dòng)設(shè)備瀏覽現(xiàn)如今各種終端越來(lái)越多,用戶可以隨時(shí)隨地在任何設(shè)備上查看優(yōu)質(zhì)的網(wǎng)頁(yè),但是這對(duì)于前端程序員來(lái)說(shuō)也是時(shí)代的考驗(yàn)。
??早年間就一臺(tái)電腦,而且顯示器就那么點(diǎn)大,怎么都好做,現(xiàn)在···說(shuō)多了全是淚。
??之前一直把在多個(gè)項(xiàng)目里面運(yùn)用相同功能的JS函數(shù)代碼,這讓每次我們都在文件中不停地復(fù)制,后來(lái)就想著把經(jīng)常能用到的函數(shù)做成代碼庫(kù)。以后的項(xiàng)目只需要引用.js文件就可以了。
??想了老半天,名字想不好,也想搞個(gè)高端、大氣、上檔次的,但是苦于“哥是個(gè)文盲”,索性把代碼貼出來(lái),反正都不是我寫(xiě)的,我就是喜歡用。

  1. 判斷瀏覽器類型
//獲取瀏覽器類型 
function getExploerName(){ 
var explorer =navigator.userAgent; var className = '';
//ie  
if (explorer.indexOf("MSIE") >= 0) { 
   className = 'ie'; 
} 
//firefox  
else if (explorer.indexOf("Firefox") >= 0) { 
   className = 'Firefox'; 
} 
//Chrome 
else if(explorer.indexOf("Chrome") >= 0){ 
   className = 'Chrome'; 
} 
//Opera
else if(explorer.indexOf("Opera") >= 0){ 
   className = 'Opera';
}
//Safari 
else if(explorer.indexOf("Safari") >= 0){ 
   className = 'Safari'; 
} 
//Netscape 
else if(explorer.indexOf("Netscape")>= 0) { 
   className = 'Netscape';
} 
   return className;
} 
  1. 判斷是不是移動(dòng)設(shè)備瀏覽
function isMobile() { 
    if((navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1) || (navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('Android') != -1)){ 
        return true; 
    }else{ 
        return false; 
    } 
}
  1. 判斷是不是微信瀏覽器
function isWeixn(){ 
      var ua = window.navigator.userAgent.toLowerCase(); 
      if(ua.match(/MicroMessenger/i)=="micromessenger") { 
        return true; 
      } else { 
        return false; 
      }
 }
  1. 判斷移動(dòng)設(shè)備橫屏or豎屏
//判斷iphone、ipad橫屏還是豎屏 
function orient() { 
    if (window.orientation == 90 || window.orientation == -90) { 
        //ipad、iphone豎屏/Andriod橫屏
        $("body").attr("class", "landscape"); orientation = 'landscape'; 
        console.log("橫屏"); 
        return false; 
    } else if (window.orientation == 0 || window.orientation == 180) {
        //ipad、iphone橫屏;Andriod豎屏 
        $("body").attr("class", "portrait"); orientation = 'portrait'; console.log("豎屏"); 
        return false; 
    } 
} 
//頁(yè)面加載時(shí)調(diào)用 
$(function () { orient(); }); 
//用戶變化屏幕方向時(shí)調(diào)用 
$(window).bind('orientationchange', function (e) { orient(); });
  1. 判斷移動(dòng)終端類型
//判斷移動(dòng)終端類型
var browser = { 
     versions:function(){ 
     var u = navigator.userAgent, app = navigator.appVersion; 
     return ; 
     }(), 
     language:(navigator.browserLanguage || navigator.language).toLowerCase()
}
document.writeln("語(yǔ)言版本: "+browser.language);
document.writeln(" 是否為移動(dòng)終端: "+browser.versions.mobile);
document.writeln(" ios終端: "+browser.versions.ios);
document.writeln(" android終端: "+browser.versions.android);
document.writeln(" 是否為iPhone: "+browser.versions.iPhone);
document.writeln(" 是否iPad: "+browser.versions.iPad);
document.writeln(navigator.userAgent);

類似的方法很多
分享一個(gè)使用實(shí)例:

if (/(iPhone|iPad|iPod|iOS)/i.test(navigator.userAgent)) {
     //alert(navigator.userAgent);  
     window.location.href ="iPhone.html";
} else if (/(Android)/i.test(navigator.userAgent)) {
     //alert(navigator.userAgent);  
     window.location.href ="Android.html";
} else { 
     window.location.href ="pc.html";
}
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

推薦閱讀更多精彩內(nèi)容