Js gets the browser version to adjust the CSS style.

Source: Internet
Author: User

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;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.