JS 判断浏览器

转自:http://blog.sina.com.cn/s/blog_5cd7f5b40100w50l.html

技术牛人写的世界上判断是否IE浏览器之最短的js代码, 
(首先一定要在服务器端运行这样谷歌才可以运行正常,其他浏览器可以非服务器端)
如下: 

Js代码  
  1. <script>   
  2. if(!+[1,])alert("这是ie浏览器");   
  3.    else alert("这不是ie浏览器");   
  4. </script>  
 
 
 
 
其他判断浏览器方法,测试通过:
代码如下:
<script language="JavaScript" type="text/javascript">
if ((navigator.userAgent.indexOf('MSIE') >= 0) && (navigator.userAgent.indexOf('Opera') < 0)){alert('你是使用IE')}else
    if (navigator.userAgent.indexOf('Firefox') >= 0){alert('你是使用Firefox')}else
        if (navigator.userAgent.indexOf('Opera') >= 0){alert('你是使用Opera')}else
{alert('你是使用其他的浏览器浏览网页!')}
</script>

或以下代码也可以

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>JS检查是什么浏览器</title>
<script language="JavaScript" type="text/javascript">
function checkFirefoxOrIE(){
userAgent=window.navigator.userAgent.toLowerCase();
if(userAgent.indexOf("firefox")>=1){
Findex=userAgent.indexOf("firefox/");
versionName=userAgent.substr(Findex+"Firefox/".length,3);
document.write("你用的是火狐浏览器!版本是:Firefox/"+versionName+"<br>");
}
else {
    var name=navigator.appName;
    if(name=="Microsoft Internet Explorer"){document.write("你用的是IE浏览器!");}
    }
}
</script>
</head>
<body onload="checkFirefoxOrIE();">
</body>
</html>

下面整理的是一套判断方法:
function getExplorer() {
var explorer = window.navigator.userAgent ;
//ie 
if (explorer.indexOf("MSIE") >= 0) {
alert("ie");
}
//firefox 
else if (explorer.indexOf("Firefox") >= 0) {
alert("Firefox");
}
//Chrome
else if(explorer.indexOf("Chrome") >= 0){
alert("Chrome");
}
//Opera
else if(explorer.indexOf("Opera") >= 0){
alert("Opera");
}
//Safari
else if(explorer.indexOf("Safari") >= 0){
alert("Safari");
}
}
 
 

猜你喜欢

转载自z3sm2012.iteye.com/blog/1882535