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吧?');
}
// 包含「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
