欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

使用JavaScript获取客户端操作系统版本和浏览器版本信息 浏览器JavaScriptChromeIEWindows 

程序员文章站 2022-06-07 21:36:54
...
    今天做的一个JSP页面中需要展示用户的浏览器版本和操作系统版本信息,这个功能在大部分论坛上都看到过,就是没考虑过怎么实现,于是求助谷老师,原来用javascript就能实现了。参考了EXT2.2的写法,自己稍微修改了一下。

var ua = navigator.userAgent.toLowerCase();

var isStrict = document.compatMode == "CSS1Compat"
isOpera = ua.indexOf("opera") > -1
isChrome = ua.indexOf("chrome") > -1
isSafari = !isChrome && (/webkit|khtml/).test(ua)
isSafari3 = isSafari && ua.indexOf('webkit/5') != -1
isIE = !isOpera && ua.indexOf("msie") > -1
isIE7 = !isOpera && ua.indexOf("msie 7") > -1
isIE8 = !isOpera && ua.indexOf("msie 8") > -1
isGecko = !isSafari && !isChrome && ua.indexOf("gecko") > -1
isGecko3 = isGecko && ua.indexOf("rv:1.9") > -1
isBorderBox = isIE && !isStrict
        
isWin7 = ua.indexOf("nt 6.1") > -1
isVista = ua.indexOf("nt 6.0") > -1
isWin2003 = ua.indexOf("nt 5.2") > -1
isWinXp = ua.indexOf("nt 5.1") > -1
isWin2000 = ua.indexOf("nt 5.0") > -1
isWindows = (ua.indexOf("windows") != -1 || ua.indexOf("win32") != -1)
isMac = (ua.indexOf("macintosh") != -1 || ua.indexOf("mac os x") != -1)
isAir = (ua.indexOf("adobeair") != -1)
isLinux = (ua.indexOf("linux") != -1)

var sys = "";
var broser = "";

if(isIE){
	broser = "IE 6";
}else if(isIE7){
	broser = "IE 7";
}else if(isIE8){
	broser = "IE 8";
}else if(isOpera){
	broser = "Opera";
}else if(isChrome){
	broser = "Chrome";
}else if(isSafari){
	broser = "Safari";
}else if(isSafari3){
	broser = "Safari3";
}else{
	broser = "Unknow";
}

if(isWin7){
	sys = "Windows 7";
}else if(isVista){
	sys = "Vista";
}else if(isWinXp){
	sys = "Windows xp";
}else if(isWin2003){
	sys = "Windows 2003";
}else if(isWin2000){
	sys = "Windows 2000";
}else if(isWindows){
	sys = "Windows";
}else if(isMac){
	sys = "Macintosh";
}else if(isAir){
	sys = "Adobeair";
}else if(isLinux){
	sys = "Linux";
}else{
	sys = "Unknow";
}

alert(sys + ":" + broser);