JS判斷各種瀏覽器類型

來源:互聯網
上載者:User

標籤:

 第一種,只區分瀏覽器,不考慮版本

    複製代碼代碼如下:

    function myBrowser(){

    var userAgent = navigator.userAgent; //取得瀏覽器的userAgent字串

    var isOpera = userAgent.indexOf("Opera") > -1;

    if (isOpera) {

    return "Opera"

    }; //判斷是否Opera瀏覽器

    if (userAgent.indexOf("Firefox") > -1) {

    return "FF";

    } //判斷是否Firefox瀏覽器

    if (userAgent.indexOf("Chrome") > -1){

    return "Chrome";

    }

    if (userAgent.indexOf("Safari") > -1) {

    return "Safari";

    } //判斷是否Safari瀏覽器

    if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) {

    return "IE";

    }; //判斷是否IE瀏覽器

    }

    //以下是調用上面的函數

    var mb = myBrowser();

    if ("IE" == mb) {

    alert("我是 IE");

    }

    if ("FF" == mb) {

    alert("我是 Firefox");

    }

    if ("Chrome" == mb) {

    alert("我是 Chrome");

    }

    if ("Opera" == mb) {

    alert("我是 Opera");

    }

    if ("Safari" == mb) {

    alert("我是 Safari");

    }

    第二種,區分瀏覽器,並考慮IE5.5 6 7 8

    複製代碼代碼如下:

    function myBrowser(){

    var userAgent = navigator.userAgent; //取得瀏覽器的userAgent字串

    var isOpera = userAgent.indexOf("Opera") > -1; //判斷是否Opera瀏覽器

    var isIE = userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera; //判斷是否IE瀏覽器

    var isFF = userAgent.indexOf("Firefox") > -1; //判斷是否Firefox瀏覽器

    var isSafari = userAgent.indexOf("Safari") > -1; //判斷是否Safari瀏覽器

    if (isIE) {

    var IE5 = IE55 = IE6 = IE7 = IE8 = false;

    var reIE = new RegExp("MSIE (\\d+\\.\\d+);");

    reIE.test(userAgent);

    var fIEVersion = parseFloat(RegExp["$1"]);

    IE55 = fIEVersion == 5.5;

    IE6 = fIEVersion == 6.0;

    IE7 = fIEVersion == 7.0;

    IE8 = fIEVersion == 8.0;

    if (IE55) {

    return "IE55";

    }

    if (IE6) {

    return "IE6";

    }

    if (IE7) {

    return "IE7";

    }

    if (IE8) {

    return "IE8";

    }

    }//isIE end

    if (isFF) {

    return "FF";

    }

    if (isOpera) {

    return "Opera";

    }

    }//myBrowser() end

    //以下是調用上面的函數

    if (myBrowser() == "FF") {

    alert("我是 Firefox");

    }

    if (myBrowser() == "Opera") {

    alert("我是 Opera");

    }

    if (myBrowser() == "Safari") {

    alert("我是 Safari");

    }

    if (myBrowser() == "IE55") {

    alert("我是 IE5.5");

    }

    if (myBrowser() == "IE6") {

    alert("我是 IE6");

    }

    if (myBrowser() == "IE7") {

    alert("我是 IE7");

    }

    if (myBrowser() == "IE8") {

    alert("我是 IE8");

    }

    下面給出一個判斷當前瀏覽器是IE的JS代碼.

    原理是利用了IE與標準瀏覽器在處理數組的toString方法的差異做成的。對於標準遊覽器,如果數組裡面最後一個字元為逗號,JS引擎會自動剔除它。

JS判斷各種瀏覽器類型

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.