Javascript学习笔记之操作系统以及浏览器验证篇

本文提供了一种使用JavaScript来检测客户端浏览器类型及操作系统的实用方法。通过解析`navigator.userAgent`属性,能够准确判断出当前使用的浏览器如IE、Firefox、Safari等,以及操作系统如Windows XP、Mac OS等。
摘要由CSDN通过智能技术生成

由于生产厂商的不同,导致个人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 " ));
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值