由于生产厂商的不同,导致个人PC的操作系统和web浏览器各有不用,这就为我们web开发从业人员带来工作上的烦恼,现正学习《Javascript高级程序设计》一书,在书中找到了相关应用,拿出来与大家分享。
// 检测浏览器类型
function Validate_Browser(str){
var sUserAgent = navigator.userAgent;
// 先要验证是否是基于KHTML的
var isKHTML = sUserAgent.indexOf( " KHTML " ) >- 1
|| sUserAgent.indexOf( " Konqueror " ) > - 1
|| sUserAgent.indexOf( " AppleWebKit " ) > - 1 ;
switch (str.toUpperCase()){
case " IE " :
return sUserAgent.indexOf( " MSIE " ) >- 1 && sUserAgent.indexOf( " compatible " ) >- 1 ;
break ;
case " FIREFOX " :
return sUserAgent.indexOf( " Gecko " ) >- 1 && ! isKHTML;
break ;
case " NETSCAPE " :
return sUserAgent.indexOf( " Mozilla " ) == 0
&& navigator.appName == " Netscape "
&& ! isKHTML;
break ;
case " OPERA " :
return sUserAgent.indexOf( " Opera " ) >- 1 ;
break ;
case " SAFARI " :
return sUserAgent.indexOf( " AppleWebKit " ) > - 1 && isKHTML;
break ;
case " KONQUEROR " :
return sUserAgent.indexOf( " Konqueror " ) > - 1 && isKHTML;
break ;
default : return false ;
}
}
// 检测操作系统
function Validate_System(str){
var sUserAgent = navigator.userAgent;
// 检测是不是WINDOWS系列的操作系统
var isWin = (navigator.platform == " Win32 " ) || (navigator.platform == " Windows " );
if (str.toUpperCase() == " WIN " )
return isWin;
if (isWin) {
switch (str.toUpperCase()){
case " WIN95 " :
return sUserAgent.indexOf( " Win95 " ) > - 1
|| sUserAgent.indexOf( " Windows 95 " ) >- 1 ;
break ;
case " WIN98 " :
return sUserAgent.indexOf( " Win98 " ) > - 1
|| sUserAgent.indexOf( " Windows 98 " ) >- 1 ;
break ;
case " WINME " :
return sUserAgent.indexOf( " Win 9x 4.90 " ) > - 1
|| sUserAgent.indexOf( " Windows ME " ) >- 1 ;
break ;
case " WIN2K " :
return sUserAgent.indexOf( " Windows NT 5.0 " ) > - 1
|| sUserAgent.indexOf( " Windows 2000 " ) >- 1 ;
break ;
case " WINXP " :
return sUserAgent.indexOf( " Windows NT 5.1 " ) > - 1
|| sUserAgent.indexOf( " Windows XP " ) >- 1 ;
break ;
case " WINNT4 " :
return sUserAgent.indexOf( " WinNT " ) > - 1
|| sUserAgent.indexOf( " Windows NT " ) >- 1
|| sUserAgent.indexOf( " WinNT4.0 " ) > - 1
|| sUserAgent.indexOf( " Windows NT 4.0 " ) > - 1
&& (
! (sUserAgent.indexOf( " Win 9x 4.90 " ) > - 1
|| sUserAgent.indexOf( " Windows ME " ) >- 1 )
&&
! (sUserAgent.indexOf( " Windows NT 5.0 " ) > - 1
|| sUserAgent.indexOf( " Windows 2000 " ) >- 1 )
&&
! (sUserAgent.indexOf( " Windows NT 5.1 " ) > - 1
|| sUserAgent.indexOf( " Windows XP " ) >- 1 )
);
break ;
default : return false ;
}
}
// 检测是不是MAC操作系统
var isMac = (navigator.platform == " Mac68K " ) || (navigator.platform == " MacPPC " );
if (isMac) {
switch (str.toUpperCase()) {
case " MAC68K " :
return sUserAgent.indexOf( " Mac_68000 " ) > - 1
|| sUserAgent.indexOf( " 68K " ) > - 1 ;
break ;
case " MACPPC " :
return sUserAgent.indexOf( " Mac_PowerPC " ) > - 1
|| sUserAgent.indexOf( " PPC " ) > - 1 ;
break ;
default : return false ;
}
}
// 检测是不是UNIX操作系统
if (str.toUpperCase() == " UNIX " )
return (navigator.platform == " Xll " ) && ! isWin && ! isMac;
}
// 调用方法
alert(Validate_System( " win " ));
alert(Validate_Browser( " ie " ));