JS判斷瀏覽器類型的方法總結,可判別當前客戶端所使用的瀏覽器是ie,firefox,safari,chrome或者是opera,另外js能夠精確判斷到ie瀏覽器的版本,依然直接上代碼,須要的朋友可按照本身的要求進行修改。php
第一種方法:html
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;
第二種方法:jquery
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 }
另外關於如何使用jquery php判斷瀏覽器類型可參照以下兩篇文章:chrome
PHP判斷瀏覽器類型瀏覽器