1. In web development, you are often asked to determine the version of the browser and the browser you are currently using, and adjust the CSS style according to the browser version,
To achieve the best effect in different browsers on the web interface, the following shows the code of the current Browser:
Copy codeThe Code is as follows:
GetBrowserVersion: function (){
Var agent = navigator. userAgent. toLowerCase ();
Var arr = [];
Var Browser = "";
Var Bversion = "";
Var verinNum = "";
// IE
If (agent. indexOf ("msie")> 0 ){
Var regStr_ie =/msie [\ d.] +;/gi;
Browser = "IE ";
Bversion = "" + agent. match (regStr_ie)
}
// Firefox
Else if (agent. indexOf ("firefox")> 0 ){
Var regStr_ff =/firefox \/[\ d.] +/gi;
Browser = "firefox ";
Bversion = "" + agent. match (regStr_ff );
}
// Chrome
Else if (agent. indexOf ("chrome")> 0 ){
Var regStr_chrome =/chrome \/[\ d.] +/gi;
Browser = "chrome ";
Bversion = "" + agent. match (regStr_chrome );
}
// Safari
Else if (agent. indexOf ("safari")> 0 & agent. indexOf ("chrome") <0 ){
Var regStr_saf =/version \/[\ d.] +/gi;
Browser = "safari ";
Bversion = "" + agent. match (regStr_saf );
}
// Opera
Else if (agent. indexOf ("opera")> = 0 ){
Var regStr_opera =/version \/[\ d.] +/gi;
Browser = "opera ";
Bversion = "" + agent. match (regStr_opera );
} Else {
Var browser = navigator. appName;
If (browser = "Netscape "){
Var version = agent. split (";");
Var trim_Version = version [7]. replace (/[]/g ,"");
Var rvStr = trim_Version.match (/[\ d \.]/g). toString ();
Var rv = rvStr. replace (/[,]/g ,"");
Bversion = rv;
Browser = "IE"
}
}
VerinNum = (Bversion + ""). replace (/[^ 0-9.]/ig ,"");
Arr. push (Browser );
Arr. push (verinNum );
Return arr;
}