First, only the browser is differentiated, regardless of the version
Copy codeThe Code is as follows:
Function myBrowser (){
Var userAgent = navigator. userAgent; // obtain the userAgent string of the browser.
Var isOpera = userAgent. indexOf ("Opera")>-1;
If (isOpera ){
Return "Opera"
}; // Determine whether the operabrowser is used
If (userAgent. indexOf ("Firefox")>-1 ){
Return "FF ";
} // Determine if the Firefox browser is used
If (userAgent. indexOf ("Chrome")>-1 ){
Return "Chrome ";
}
If (userAgent. indexOf ("Safari")>-1 ){
Return "Safari ";
} // Determine whether the browser is Safari
If (userAgent. indexOf ("compatible")>-1 & userAgent. indexOf ("MSIE")>-1 &&! IsOpera ){
Return "IE ";
}; // Determines whether the IE browser is used
}
// The following is a call to the above function
Var mb = myBrowser ();
If ("IE" = mb ){
Alert ("I am IE ");
}
If ("FF" = mb ){
Alert ("I am Firefox ");
}
If ("Chrome" = mb ){
Alert ("Chrome ");
}
If ("Opera" = mb ){
Alert ("My name is Opera ");
}
If ("Safari" = mb ){
Alert ("I am Safari ");
}
Second, differentiate browsers and consider IE5.5 6 7 8
Copy codeThe Code is as follows:
Function myBrowser (){
Var userAgent = navigator. userAgent; // obtain the userAgent string of the browser.
Var isOpera = userAgent. indexOf ("Opera")>-1; // determines whether the operabrowser is used.
Var isIE = userAgent. indexOf ("compatible")>-1 & userAgent. indexOf ("MSIE")>-1 &&! IsOpera; // determines whether the IE browser is used
Var isFF = userAgent. indexOf ("Firefox")>-1; // determine whether the Firefox browser is used
Var isSafari = userAgent. indexOf ("Safari")>-1; // determine whether the browser is Safari
If (isIE ){
Var IE5 = IE55 = IE6 = IE7 = IE8 = false;
Var reIE = new RegExp ("MSIE (\ d + \. \ d + );");
ReIE. test (userAgent );
Var fIEVersion = parseFloat (RegExp ["$1"]);
IE55 = fIEVersion = 5.5;
IE6 = fIEVersion = 6.0;
IE7 = fIEVersion = 7.0;
IE 8 = fIEVersion = 8.0;
If (IE55 ){
Return "IE55 ";
}
If (IE6 ){
Return "IE6 ";
}
If (IE7 ){
Return "IE7 ";
}
If (IE8 ){
Return "IE8 ";
}
} // IsIE end
If (isFF ){
Return "FF ";
}
If (isOpera ){
Return "Opera ";
}
} // MyBrowser () end
// The following is a call to the above function
If (myBrowser () = "FF "){
Alert ("I am Firefox ");
}
If (myBrowser () = "Opera "){
Alert ("My name is Opera ");
}
If (myBrowser () = "Safari "){
Alert ("I am Safari ");
}
If (myBrowser () = "IE55 "){
Alert ("I'm IE5.5 ");
}
If (myBrowser () = "IE6 "){
Alert ("I'm IE6 ");
}
If (myBrowser () = "IE7 "){
Alert ("I am IE7 ");
}
If (myBrowser () = "IE8 "){
Alert ("I am IE8 ");
}
The following is a javascript code used to determine whether the browser is IE.
The principle is based on the difference between IE and the standard browser in handling the toString method of arrays. For the standard browser, if the last character in the array is a comma, The JS engine will automatically remove it.
[Ctrl + A select all Note: If you need to introduce external Js, You need to refresh it to execute]