JS判断浏览器类型的方法总结,可判别当前客户端所使用的浏览器是ie,firefox,safari,chrome或者是opera,另外js可以精确判断到ie浏览器的版本,依然直接上代码,需要的朋友可按照自己的要求进行修改。
第一种方法:
1 var Browser=new Object();
2 Browser.isMozilla=(typeof document.implementation!='undefined')&&(typeof document.implementation.createDocument!='undefined')&&(typeof HTMLDocument!='undefined');
3 Browser.isIE=window.ActiveXObject ? true : false;
4 Browser.isFirefox=(navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
5 Browser.isSafari=(navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
6 Browser.isOpera=(navigator.userAgent.toLowerCase().indexOf("opera")!=-1);
7 function check(){
8 alert(Browser.isIE?'ie':'not ie');
9 alert(Browser.isFirefox?'Firefox':'not Firefox');
10 alert(Browser.isSafari?'Safari':'not Safari');
11 alert(Browser.isOpera?'Opera':'not Opera');
12 }
13 window.onload=check;
第二种方法:
1 function isBrowser(){
2 var Sys={};
3 var ua=navigator.userAgent.toLowerCase();
4 var s;
5 (s=ua.match(/msie ([\d.]+)/))?Sys.ie=s[1]:
6 (s=ua.match(/firefox\/([\d.]+)/))?Sys.firefox=s[1]:
7 (s=ua.match(/chrome\/([\d.]+)/))?Sys.chrome=s[1]:
8 (s=ua.match(/opera.([\d.]+)/))?Sys.opera=s[1]:
9 (s=ua.match(/version\/([\d.]+).*safari/))?Sys.safari=s[1]:0;
10 if(Sys.ie){//Js判断为IE浏览器
11 alert('http://www.phpernote.com'+Sys.ie);
12 if(Sys.ie=='9.0'){//Js判断为IE 9
13 }else if(Sys.ie=='8.0'){//Js判断为IE 8
14 }else{
15 }
16 }
17 if(Sys.firefox){//Js判断为火狐(firefox)浏览器
18 alert('http://www.phpernote.com'+Sys.firefox);
19 }
20 if(Sys.chrome){//Js判断为谷歌chrome浏览器
21 alert('http://www.phpernote.com'+Sys.chrome);
22 }
23 if(Sys.opera){//Js判断为opera浏览器
24 alert('http://www.phpernote.com'+Sys.opera);
25 }
26 if(Sys.safari){//Js判断为苹果safari浏览器
27 alert('http://www.phpernote.com'+Sys.safari);
28 }
29 }