判斷瀏覽器類型
[javascript] view plain copy 在CODE上查看代碼片派生到我的代碼片
if ( window.sidebar && "object" == typeof( window.sidebar ) && "function" == typeof( window.sidebar.addPanel ) ) // firefox
{
}
else if ( document.all && "object" == typeof( window.external ) ) // ie
{
}
js用來區(qū)別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瀏覽器”);}下面是區(qū)別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的情況下土全,進(jìn)行了在一次的檢測,如果你對此有懷疑会涎,可以測試一下裹匙。我這里就直接使用在判斷中了,你也可以將他們先進(jìn)行聲明成變量進(jìn)行使用末秃。據(jù)說火狐以后也會加入document.all這個方法幻件,所以建議使用第二種方法,應(yīng)該會安全一些蛔溃。
用navigator.userAgent.indexOf()來區(qū)分多瀏覽器,代碼示例如下:
[javascript] view plain copy 在CODE上查看代碼片派生到我的代碼片
<coding-1 lang="other">
<script type="text/javascript">
var browser={
versions:function(){
var u = navigator.userAgent, app = navigator.appVersion;
return {
trident: u.indexOf('Trident') > -1, //IE內(nèi)核
presto: u.indexOf('Presto') > -1, //opera內(nèi)核
webKit: u.indexOf('AppleWebKit') > -1, //蘋果篱蝇、谷歌內(nèi)核
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //火狐內(nèi)核
mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //是否為移動終端
ios: !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/), //ios終端
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android終端或者uc瀏覽器
iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //是否為iPhone或者QQHD瀏覽器
iPad: u.indexOf('iPad') > -1, //是否iPad
webApp: u.indexOf('Safari') == -1 //是否web應(yīng)該程序贺待,沒有頭部與底部
};
}()
}
document.writeln(" 是否為移動終端: "+browser.versions.mobile);
document.writeln(" ios終端: "+browser.versions.ios);
document.writeln(" android終端: "+browser.versions.android);
document.writeln(" 是否為iPhone: "+browser.versions.iPhone);
document.writeln(" 是否iPad: "+browser.versions.iPad);
document.writeln(navigator.userAgent);
</script>
</coding>