Home > Article > Web Front-end > How to get the browser type in Js
This article mainly shares with you how to obtain the browser type in Js. It is mainly shared with you in the form of code. I hope it can help you.
<script> function myBrowser(){ var userAgent = navigator.userAgent; //取得浏览器的userAgent字符串 var isOpera = userAgent.indexOf("Opera") > -1; //判断是否Opera浏览器 if (isOpera) { return "Opera" }; //判断是否Firefox浏览器 if (userAgent.indexOf("Firefox") > -1) { return "FF"; } //判断是否Chrome浏览器 if (userAgent.indexOf("Chrome") > -1){ return "Chrome"; } //判断是否Safari浏览器 if (userAgent.indexOf("Safari") > -1) { return "Safari"; } //判断是否IE浏览器 if (userAgent.indexOf("compatible") > -1 && userAgent.indexOf("MSIE") > -1 && !isOpera) { return "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"); } </script>
Related recommendations:
jQuery method of obtaining the browser type and version number
js obtains the browser and screen Various widths and heights
Simple JS to get the browser IP code
The above is the detailed content of How to get the browser type in Js. For more information, please follow other related articles on the PHP Chinese website!