当前位置: 代码迷 >> JavaScript >> 经过javascript检测客户端环境
  详细解决方案

经过javascript检测客户端环境

热度:108   发布时间:2012-11-25 11:44:31.0
通过javascript检测客户端环境

?

?? 通过javascript,判断客户端的环境,诸如使用的浏览器和操作系统。

?? user-agent字符串检测法是最原始的浏览器检测方式,每个访问网站的程序都必须提供一个user-agent字符来向服务器确定他的身份。
?? navigator对象包含一些浏览器信息,诸如名称、版本号和平台。

?

?? 以下这段代码是从“javascript 高级程序设计”抄出来的。可能有敲错的地方,具体没有测试,用的时候大概调试下哈。

//检测客户端环境
var sUserAgent = navigator.userAgent;
var fAppVersion = parseFloat(navigator.appVersion); //当前版本

/**
*	比较两个版本的新旧情况
*	参数:	sVersion1,sVersion2 版本号,字符串
*	返回值:sVersion1比sVersion2 大,返回1
*			sVersion1比sVersion2 大,返回-1
*			sVersion1与sVersion2一样,返回0
*/
function compareVersions(sVersion1,sVersion2){
	var aVersion1 = sVersion1.split(".");
	var aVersion2 = sVersion2.split(".");
	
	//版本号位数不一样,在后面补0
	if(aVersion1.length > aVersion2.length){
		for(var i=0; i < aVersion1.length - aVersion2.length; i++){
			aVersion2.push("0");
		}	
	}else if(aVersion1.length < aVersion2.length){
		for(var i=0; i < aVersion2.length - aVersion1.length; i++){
			aVersion1.push("0");
		}	
	}
	for(var i=0; i < aVersion1.length; i++){
		if(aVersion1[i] < aVersion2[i]){
			return -1;
		}else if(aVersion1[i] > aVersion2[i]){
			return 1;
		}
	}
	
	return 0;
	
}

var isOpera = sUserAgent.indexOf("Opera") > -1;//判断是否是Opera浏览器
var isMinOpera4 = isMinOpera5 = isMinOpera6 = isMinOpera7 = isMinOpera7_5 = false;//最低版本号

if(isOpera){
	var fOperaVersion;
	//因为Opera会伪装成其他浏览器,所以通过两种方式检测
	if(navigator.appName == "Opera"){//没有伪装
		fOperaVersion = fAppVersion;
	}else{//
		var reOperaVersion = new RegExp("Opera (\\d+\\.\\d+)");
		reOperaVersion.test(sUserAgent);
		fOperaVersion = parseFloat(RegExp["$1"]);
	}
	
	isMinOpera4 = fOperaVersion >= 4;
	isMinOpera5 = fOperaVersion >= 5;
	isMinOpera6 = fOperaVersion >= 6;
	isMinOpera7 = fOperaVersion >= 7;
	isMinOpera7_5 = fOperaVersion >= 7.5;
} 

var isKHTML = sUserAgent.indexOf("KHTML") > -1
				|| sUserAgent.indexOf("Konqueror") > -1
				|| sUserAgent.indexOf("AppleWebKit") > -1;

var isMinSafari1 = isMinSafari_2 = false;
var isMinKonq2_2 = isMinKonq3 = isMinKonq3_1 = isMinKonq3_2 = false;

if(isKHTML){
	isSafari = sUserAgent.indexOf("AppleWebKit") > -1;
	isKonq = sUserAgent.indexOf("Konqueror") > -1;
	
	if(isSafari){
		var reAppleWebKit = new RegExp("AppleWebKit\\/(\\d+(?:\\.\\d*)?)");
		reAppleWebKit.test(sUserAgent);
		var fAppleWebKitVersion = parseFloat(RegExp["$1"]);
	}else if(isKonq){
		var reKonq = new RegExp("Konqueror\\/(\\d+(?:\\.\\d+(?:\\.\\d)?)?)");
		reKonq.test(sUserAgent);
		isMinKonq2_2 = compareVersions(RegExp["$1"],"2.2") >= 0;
		isMinKonq3 = compareVersions(RegExp["$1"],"3.0") >= 0;
		isMinKonq3_1 = compareVersions(RegExp["$1"],"3.1") >= 0;
		isMinKonq3_2 = compareVersions(RegExp["$1"],"3.2") >= 0;
	}
}			

var isIE = sUserAgent.indexOf("compatible") > -1
			&& sUserAgent.indexOf("MSIE") > -1
			&& !isOpera;
var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = isMinIE7 = isMinIE8 = false;
if(isIE){
	var reIE = new RegExp("MSIE (\\d+\\.\\d+);");
	reIE.test(sUserAgent);
	var fIEVersion = parseFloat(RegExp["$1"]);
	
	isMinIE4 = fIEVersion >= 4;
	isMinIE5 = fIEVersion >= 5;
	isMinIE5_5 = fIEVersion >= 5.5;
	isMinIE6 = fIEVersion >= 6;
	isMinIE7 = fIEVersion >= 7;
	isMinIE8 = fIEVersion >= 8;
}

var isMoz = sUserAgent.indexOf("Gecko") > -1 
			&& !isKHTML;
var isMinMoz1 = isMinMoz1_4 = isMinMoz1_5 = false;

if(isMoz){
	var reMoz = new RegExp("rv:(\\d+\\.\\d+(?:\\.\\d+)?)");
	reMoz.test(sUserAgent);
	isMinMoz1 = compareVersions(RegExp["$1"],"1.0") >= 0;
	isMinMoz1_4 = compareVersions(RegExp["$1"],"1.4") >= 0;
	isMinMoz1_5 = compareVersions(RegExp["$1"],"1.5") >= 0;
}

var isNS4 = !isIE && !isOpera && !isMoz && !isKHTML
			&& (sUserAgent.indexOf("Mozilla") == 0
			&& (navigator.appName == "Netscape");
			&& (fAppVersion > 4.0 && fAppVersion < 5.0);
			
var isMinNS4 = isMinNS4_5 = isMinNS4_7 = isMinNS4_8 = false;

if(isNS4){
	isMinNS4 = true;
	isMinNS4_5 = fAppVersion >= 4.5;
	isMinNS4_7 = fAppVersion >= 4.7;
	isMinNS4_8 = fAppVersion >= 4.8;
}


//以下是检测操作系统的
var isWin = (navigator.platform == "Win32") || (navigator.platform == "Windows");
var isMac = (navigator.platform == "Mac68K") || (navigator.platform == "MacPPC") || (navigator.platform == "Macintosh"); 

var isUnix = (navigator.platform == "X11") && !isWin && !isMac;
var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP = isWinVi = isWin7 = false;

if(isWin){
	isWin95 = sUserAgent.indexOf("Win95") > -1 || sUserAgent.indexOf("Windows 95") > -1;
	isWin98 = sUserAgent.indexOf("Win98") > -1 || sUserAgent.indexOf("Windows 98") > -1;
	isWinME = sUserAgent.indexOf("Win 9x 4.90") > -1 || sUserAgent.indexOf("Windows ME") > -1;
	isWin2K = sUserAgent.indexOf("Win NT 5.0") > -1 || sUserAgent.indexOf("Windows 2000") > -1;
	isWinXP = sUserAgent.indexOf("Win NT 5.1") > -1 || sUserAgent.indexOf("Windows XP") > -1;
	isWinVi = sUserAgent.indexOf("Windows NT 6.0") > -1  || sUserAgent.indexOf("Windows Vista") > -1;
	isWin7 = sUserAgent.indexOf("Windows NT 6.1") > -1  || sUserAgent.indexOf("Windows 7") > -1;
	isWinNT = sUserAgent.indexOf("WinNT") > -1 || sUserAgent.indexOf("Windows NT") > -1
				|| sUserAgent.indexOf("WinNT4.0") > -1 || sUserAgent.indexOf("Windows NT 4.0") > -1
				&& (!isWinME && !isWin2K && !isWinXP && !isWinVi && !isWin7);
}

if(isMac){
	isMac68K = sUserAgent.indexOf("Mac_68000") > -1 || sUserAgent.indexOf("68K") > -1;
	isMacPPC = sUserAgent.indexOf("Mac_PowerPC") > -1 || sUserAgent.indexOf("PPC") > -1;
}

if(isUnix){
	isSunOS = sUserAgent.indexOf("SunOS") > -1;
	if(isSunOS){
		var reSunOS = new RegExp("SunOS (\\d+\\.\\d+(?:\\.\\d+)?)");
		reSunOs.test(sUserAgent);
		isMinSunOS4 = compareVersions(RegExp["$1"],"4.0") >= 0;
		isMinSunOS5 = compareVersions(RegExp["$1"],"5.0") >= 0;
		isMinSunOS5_5 = compareVersions(RegExp["$1"],"5.5") >= 0;
	}
}
  相关解决方案