Traditionally, the User-agent detection method is used to detect the exact model of the target browser, including the name and version of the browser. is actually a string, obtained with Navigator.useragen or navigator.appname. As follows:
function Isie () {
Return Navigator.appName.indexOf ("Microsoft Internet Explorer")!=-1 && document.all;
}
function IsIE6 () {
return Navigator.userAgent.split (";") [1].tolowercase (). IndexOf ("MSIE 6.0") = = "1"? False:true;
}
function IsIE7 () {
return Navigator.userAgent.split (";") [1].tolowercase (). IndexOf ("MSIE 7.0") = = "1"? False:true;
}
function IsIE8 () {
return Navigator.userAgent.split (";") [1].tolowercase (). IndexOf ("MSIE 8.0") = = "1"? False:true;
}
function Isnn () {
Return navigator.userAgent.indexOf ("Netscape")!=-1;
}
function Isopera () {
Return navigator.appName.indexOf ("Opera")!=-1;
}
function Isff () {
Return navigator.userAgent.indexOf ("Firefox")!=-1;
}
function Ischrome () {
Return navigator.userAgent.indexOf ("Chrome") >-1;
}
At present, the most popular detection method is through "object/feature" to determine the way the browser capabilities. The following addevent code is used in this way ... AddEventListener is the standard format for the DOM, while IE uses its own unique attachevent.
Add Event Tool function
function Addevent (el,type,handle) {
if (el.addeventlistener) {//for standard browses
El.addeventlistener (Type,handle,false);
}else if (el.attachevent) {//for IE
El.attachevent ("on" +event,handle);
}else{//other
el["on" +type]=handle;
}
}
In lifting several columns:
1, Talbe.cells only Ie/opera support.
2, innertext/insertadjacenthtml In addition to Firefox, Ie6/7/8/safari/chrome/opera are supported.
3, Window.external.AddFavorite is used to add to the favorites under IE.
4, Window.sidebar.addPanel is used to add to the favorites under Firefox.
Another interesting way to detect this is to call it a "browser flaw or bug" approach, where some of the performance is not a deliberate implementation by the browser vendor. As follows:
var Isie =!+ "V1";
var Isie =!-[1,];
var Isie = "V" = = "V";
issafari=/a/.__proto__== '//';
isopera=!! Window.opera;
Isie = "V" = = "V" Way IE9 has fixed the bug, can't judge IE browser in this way (2010-6-29 preview of the third edition with IE9 platform)
The most classic is!-[1,] the judgment way, only uses 6 bytes to determine IE browser, creates the world shortest record, this group of code utilizes the array [1,] the length.
IE Browser detection code:
// ----------------------------------------------------------
A short snippet for detecting versions of IE in JavaScript
Without resorting to user-agent sniffing
// ----------------------------------------------------------
If you are not in IE (or IE version is less than 5) then:
ie = = undefined
If you are ' re in IE (>=5) then can determine which version:
ie = = 7; IE7
Thus, to detect IE:
if (IE) {}
and to detect the version:
ie = = 6//IE6
IE > 7//IE8, IE9 ...
IE < 9//anything less than IE9
// ----------------------------------------------------------
Update:now using Live nodelist idea from @jdalton
var ie = (function () {
var undef,
v = 3,
div = document.createelement (' div '),
all = Div.getelementsbytagname (' i ');
while (
div.innerhtml = ' <!--[if GT IE ' + (++v) + ']><! [endif]--> ',
ALL[0]
);
Return v > 4? V:undef;
})();
The intent of this code is. Through the while loop. Writes conditional compilation to a div that is a page, and then takes the element directly under the annotation, and exits the loop until it is not. This will get the final version of IE, if not ie is undefined.