js判斷瀏覽器類型

來源:互聯網
上載者:User

標籤:iphone   注意   null   瀏覽器   string   web   ref   使用   match   

使用navigator.userAgent和來判斷

PC端:

<script type="text/javascript">
var ua=navigator.userAgent.toLowerCase();
if(ua.match(/webkit/i)){
alert("chrome");
}else if(ua.match(/firefox/i)){
alert("firefox");
}
else if(ua.match(/opera/i)){
alert("opera");
}else if(ua.match("trident")){
alert("IE");
}
</script>

移動端:

<script type="text/javascript">
var ua=navigator.userAgent.toLowerCase();
var neihe=document.getElementById(‘neihe‘);
if(ua.match(/qq/i)){
neihe.innerHTML="QQ";
}else if(ua.match(/microMessenger/i)){
neihe.innerHTML="";
}else if(ua.match(/oppo/i)){
neihe.innerHTML="OPPO";
}else if(ua.match(/iphone/i)||ua.match(/ipad/i)){
neihe.innerHTML="safari";
}else if(ua.match(/opera/i)){
neihe.innerHTML="opera";
}
</script>

 

關於navigator.userAgent,可參考 http://www.runoob.com/jsref/prop-nav-useragent.html

注意: match() 方法將檢索字串 String Object,以找到一個或多個與 regexp 匹配的文本。這個方法的行為在很大程度上有賴於 regexp 是否具有標誌 g。如果 regexp 沒有標誌 g,那麼 match() 方法就只能在 stringObject 中執行一次匹配。如果沒有找到任何匹配的文本, match() 將返回 null。否則,它將返回一個數組,其中存放了與它找到的匹配文本有關的資訊。

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.