JS determine what type of browser
Copy Code code as follows:
if (Window.sidebar && "object" = = typeof (Window.sidebar) && "function" = = typeof (Window.sidebar.addPan EL))//Firefox
{
}
else if (document.all && "object" = = typeof (window.external))//IE
{
}
JS is used to distinguish between IE and other browsers and Ie6-8 methods.
1, document.all
2 、!! Window. ActiveXObject;
Use the following methods:
if (document.all) {
Alert ("IE browser");
}else{
Alert ("Non IE browser");
}
if (!! Window. ActiveXObject) {
Alert ("IE browser");
}else{
Alert ("Non IE browser");
}
Here are the ways to differentiate 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 of all we make sure that this browser is in the case of IE, conducted at a time of detection, if you have doubts about this, you can test.
I use this directly in judgment, and you can also declare them as variables to use first. It is said that Firefox will also join document.all this way, so it is recommended that the second method should be safer.
Using Navigator.userAgent.indexOf () to differentiate multiple browsers, the code example is as follows:
Copy 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 for iphone or Qqhd browser
Ipad:u.indexof (' ipad ') >-1,//whether the ipad
Webapp:u.indexof (' Safari ') = = 1/Whether the Web should program, without head and bottom
};
}()
}
Document.writeln ("Whether for Mobile Terminal:" +browser.versions.mobile);
Document.writeln ("iOS terminal:" +browser.versions.ios);
Document.writeln ("Android Terminal:" +browser.versions.android);
Document.writeln ("Whether for IPhone:" +browser.versions.iphone);
Document.writeln ("Whether the IPad:" +browser.versions.ipad);
Document.writeln (navigator.useragent);
</script>
</coding>
JavaScript, whether it's a PC browser or a mobile browser, is judged by the user Agent.