js determines what type of browser it is
Copy the code code as follows:
if ( window.sidebar && "object" == typeof( window.sidebar ) && "function" == typeof( window.sidebar.addPanel ) ) // firefox
{
}
else if ( document.all && "object" == typeof( window.external ) ) // ie
{
}
js is used to distinguish IE from other browsers and between IE6-8.
1.document.all
2. !!window.ActiveXObject;
How to use it:
if (document.all){
alert("IE browser");
}else{
alert("Non-IE browser");
}
if (!!window.ActiveXObject){
alert("IE browser");
}else{
alert("Non-IE browser");
}
The following is how to distinguish between IE6, IE7, and 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");
}
}
First, we make sure that the browser is IE and have been tested once. If you have doubts about this, you can test it.
I will use them directly in judgment here. You can also declare them as variables first for use. It is said that Firefox will also add the document.all method in the future, so it is recommended to use the second method, which should be safer.
Use navigator.userAgent.indexOf() to distinguish multiple browsers. The code example is as follows:
Copy the code code as follows:
<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 kernel
presto: u.indexOf('Presto') > -1, //opera kernel
webKit: u.indexOf('AppleWebKit') > -1, //Apple, Google kernel
gecko: u.indexOf('Gecko') > -1 && u.indexOf('KHTML') == -1, //Firefox kernel
mobile: !!u.match(/AppleWebKit.*Mobile.*/)||!!u.match(/AppleWebKit/), //Whether it is a mobile terminal
ios: !!u.match(//(i[^;]+;( U;)? CPU.+Mac OS X/), //ios terminal
android: u.indexOf('Android') > -1 || u.indexOf('Linux') > -1, //android terminal or uc browser
iPhone: u.indexOf('iPhone') > -1 || u.indexOf('Mac') > -1, //Whether it is iPhone or QQHD browser
iPad: u.indexOf('iPad') > -1, //Whether it is iPad
webApp: u.indexOf('Safari') == -1 //Whether the web should be a program without header and bottom
};
}()
}
document.writeln(" Whether it is a mobile terminal: "+browser.versions.mobile);
document.writeln(" ios terminal: "+browser.versions.ios);
document.writeln(" android terminal: "+browser.versions.android);
document.writeln(" Whether it is iPhone: "+browser.versions.iPhone);
document.writeln(" Whether iPad: "+browser.versions.iPad);
document.writeln(navigator.userAgent);
</script>
</coding>
Whether JavaScript is judging a PC browser or a mobile browser, it is judged through the User Agent.