为了解决网站浏览器兼容的问题,有时候需要获取客户端使用的浏览器类型,通常我们可以通过分析浏览器的userAgent来判别浏览器类型,下面提供两种使用JS判断浏览器类型的方法,可判别当前客户端所使用的浏览器是ie,firefox,safari,chrome或者是opera,另外可以精确判断到ie浏览器的版本,需要的朋友可按照自己的要求进行修改。

第一种方法
var Browser=new Object();
Browser.isMozilla=(typeof document.implementation!='undefined')&&(typeof document.implementation.createDocument!='undefined')&&(typeof HTMLDocument!='undefined');
Browser.isIE=window.ActiveXObject ? true : false;
Browser.isFirefox=(navigator.userAgent.toLowerCase().indexOf("firefox")!=-1);
Browser.isSafari=(navigator.userAgent.toLowerCase().indexOf("safari")!=-1);
Browser.isOpera=(navigator.userAgent.toLowerCase().indexOf("opera")!=-1);
function check(){
	alert(Browser.isIE?'ie':'not ie');
	alert(Browser.isFirefox?'Firefox':'not Firefox');
	alert(Browser.isSafari?'Safari':'not Safari');
	alert(Browser.isOpera?'Opera':'not Opera');
}
window.onload=check;

第二种方法
function isBrowser(){
	var Sys={};
	var ua=navigator.userAgent.toLowerCase();
	var s;
	(s=ua.match(/msie ([\d.]+)/))?Sys.ie=s[1]:
	(s=ua.match(/firefox\/([\d.]+)/))?Sys.firefox=s[1]:
	(s=ua.match(/chrome\/([\d.]+)/))?Sys.chrome=s[1]:
	(s=ua.match(/opera.([\d.]+)/))?Sys.opera=s[1]:
	(s=ua.match(/version\/([\d.]+).*safari/))?Sys.safari=s[1]:0;
	if(Sys.ie){//Js判断为IE浏览器
		alert('http://www.junphp.com'+Sys.ie);
		if(Sys.ie=='9.0'){//Js判断为IE 9
		}else if(Sys.ie=='8.0'){//Js判断为IE 8
		}else{
		}
	}
	if(Sys.firefox){//火狐(firefox)浏览器
		alert('http://www.junphp.com'+Sys.firefox);
	}
	if(Sys.chrome){//谷歌chrome浏览器
		alert('http://www.junphp.com'+Sys.chrome);
	}
	if(Sys.opera){//opera浏览器
		alert('http://www.junphp.com'+Sys.opera);
	}
	if(Sys.safari){//safari浏览器
		alert('http://www.junphp.com'+Sys.safari);
	}
}


相关评论(0)
您是不是忘了说点什么?

友情提示:垃圾评论一律封号...

还没有评论,快来抢沙发吧!