实用的js判断浏览器类型及版本

代码:

 1 <html xmlns="http://www.w3.org/1999/xhtml" >
2 <head>
3 <title>实用的js判断浏览器类型及版本</title>
4 <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
5 <script language="JavaScript">
6 var imyeah={};
7 var ua=navigator.userAgent.toLowerCase();
8 var s;
9 (s=ua.match(/msie ([\d.]+)/)) ? imyeah.ie=s[1] :
10 (s=ua.match(/firefox\/([\d.]+)/)) ? imyeah.firefox=s[1] :
11 (s=ua.match(/chrome\/([\d.]+)/)) ? imyeah.chrome=s[1] :
12 (s=ua.match(/opera.([\d.]+)/)) ? imyeah.opera=s[1] :
13 (s=ua.match(/version\/([\d.]+).*safari/)) ? imyeah.safari=s[1] : 0;
14
15 //以下进行测试
16 if(imyeah.ie) document.write('IE: '+imyeah.ie);
17 if(imyeah.firefox) document.write('Firefox: '+imyeah.firefox);
18 if(imyeah.chrome) document.write('Chrome: '+imyeah.chrome);
19 if(imyeah.opera) document.write('Opera: '+imyeah.opera);
20 if(imyeah.safari) document.write('Safari: '+imyeah.safari);
21 </script>
22 </head>
23 <body>
24 </body>
25 </html>

这段代码非常简短,但能够准确判断ie、FF、Chrome、Opera、Safari浏览器及其版本,非常实用。

原文地址:https://www.cnblogs.com/imyeah/p/2298793.html