js 判断是什么类型浏览器

一、js 判断是什么类型浏览器

//  firefox
if ( window.sidebar && "object" == typeof( window.sidebar ) && "function" == typeof( window.sidebar.addPanel ) ) 
{  

} 
//  ie
else if ( document.all && "object" == typeof( window.external ) )
 {    

}

二、js用来区别IE与其他浏览器及IE6-8之间的方法。

1、document.all 
2、!!window.ActiveXObject;

使用方法如下:

if (document.all){ alert(”IE浏览器”); }else{ alert(”非IE浏览器”); }
if (!!window.ActiveXObject){ alert(”IE浏览器”); }else{ alert(”非IE浏览器”); }

下面是区别IE6、IE7、IE8之间的方法:

var isIE=!!window.ActiveXObject; 
var isIE6=isIE&&!window.XMLHttpRequest; 
var isIE8=isIE&&!!document.documentMode; 
var isIE7=isIE&&!isIE6&&!isIE8; 
if (isIE)
{ 
      if (isIE6)
      { 
           alert(”ie6″); 
       }
      else if (isIE8)
       {
           alert(”ie8″); 
       }else if (isIE7)
      { 
           alert(”ie7″); 
      } 
}

首先我们确保这个浏览器为IE的情况下,进行了在一次的检测,如果你对此有怀疑,可以测试一下。
我这里就直接使用在判断中了,你也可以将他们先进行声明成变量进行使用。据说火狐以后也会加入document.all这个方法,所以建议使用下面一种方法,应该会安全一些。

三、用navigator.userAgent.indexOf()来区分多浏览器,代码示例如下:

<html> 
<head> 
<title>TAG index Web</title> 
</head> 
<body> 
<script type="text/javascript"> 
document.write('浏览器判別:');
// 包含「Opera」文字列 
if(navigator.userAgent.indexOf("Opera") != -1) 
{ 
     document.write('您的浏览器是Opera吧?'); 
} 
// 包含「MSIE」文字列 
else if(navigator.userAgent.indexOf("MSIE") != -1) 
{ 
     document.write('您的浏览器是Internet Explorer吧?'); 
} 
// 包含「Firefox」文字列 
else if(navigator.userAgent.indexOf("Firefox") != -1) 
{ 
    document.write('您的浏览器时Firefox吧?'); 
}
// 包含「Netscape」文字列 
else if(navigator.userAgent.indexOf("Netscape") != -1)
{ 
    document.write('您的浏览器时Netscape吧?'); 
}  
// 包含「Chrome」文字列 
 else if(navigator.userAgent.indexOf("Chrome") != -1) 
{ 
      document.write('您的浏览器时Chrome 吧?'); 
} 
// 包含「Safari」文字列 
else if(navigator.userAgent.indexOf("Safari") != -1) 
{ 
     document.write('您的浏览器时Safari 吧?'); 
} 
else{ 
   document.write('无法识别的浏览器。'); 
} 
document.write(''); 
</script> 
</body> 
</html>

转自:http://hi.baidu.com/sqq_qqs/item/b0c508c2e960193c0831c676

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值