標籤: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判斷瀏覽器類型