This article mainly introduces how to use JavaScript to determine whether the access source is a mobile phone or a computer, and which browser is used. For more information, see.
Js determines what type of Browser
The Code is 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 methods between IE6-8.
1. document. all
2 ,!! Window. ActiveXObject;
The usage is as follows:
If (document. all ){
Alert ("IE browser ");
} Else {
Alert ("non-IE browser ");
}
If (!! Window. ActiveXObject ){
Alert ("IE browser ");
} Else {
Alert ("non-IE browser ");
}
The following describes how to differentiate IE6, IE7, and IE8:
Var isIE = !! Window. ActiveXObject;
Var isIE6 = isIE &&! Window. XMLHttpRequest;
Var isIE8 = isIE &&!! Document.doc umentMode;
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 this browser is IE and has been tested once. If you have doubts about this, you can test it.
I am using it directly here. You can declare them as variables for use. It is said that Firefox will also add the document. all method in the future. Therefore, it is recommended to use the second method, which should be safer.
Use navigator. userAgent. indexOf () to differentiate multiple browsers. The code example is as follows:
The Code is as follows: