JavaScript implementation get Browser version, browser type _javascript tips

Source: Internet
Author: User

Find a good code for using JavaScript to judge browsers and browser versions from the Web, and record this here:

<script type= "Text/javascript" >
    var Sys = {};
    var ua = Navigator.userAgent.toLowerCase ();
    var s;
    (s = Ua.match (/msie) ([\d.] +)/)) ? sys.ie = s[1]:
    (s = Ua.match (/firefox\/([\d.] +)/)) ? Sys.firefox = s[1]:
    (s = Ua.match (/chrome\/([\d.] +)/)) ? Sys.chrome = s[1]:
    (s = Ua.match (/opera. ( [\d.] +)/)) ? Sys.opera = s[1]:
    (s = Ua.match (/version\/([\d.] +). *safari/)? Sys.safari = S[1]: 0;

    The following test
    if (sys.ie) document.write (' ie: ' + sys.ie);
    if (Sys.firefox) document.write (' Firefox: ' + sys.firefox);
    if (sys.chrome) document.write (' Chrome: ' + sys.chrome);
    if (Sys.opera) document.write (' Opera: ' + Sys.opera);
    if (Sys.safari) document.write (' Safari: ' + Sys.safari);
</script>

Encapsulate the above code as a method that returns the Sys object, which encapsulates the browser's type and version information as follows:

function Getbrowserinfo () {
  var Sys = {};
  var ua = Navigator.userAgent.toLowerCase ();
  var re =/(msie|firefox|chrome|opera|version). *? ([\d.] +)/;
  var m = Ua.match (re);
  Sys.browser = M[1].replace (/version/, "Safari");
  Sys.ver = m[2];
  return Sys;
}

When you need to get the browser's type and version information, you can use the Getbroserinfo method, as follows:

 <script type= "Text/javascript" >
     //Get current browser information
     var sys = getbrowserinfo ();
     Sys.browser get the type of browser, sys.ver get Browser version
     document.write (Sys.browser + "version is:" + sys.ver);
 </script>

The complete test code is as follows:

<! DOCTYPE html>  

Run Result:

IE Browser Test results:

  

Google Browser Test results:

  

Firefox Browser test results:

The above is about JavaScript implementation get browser version and type of detailed code, for IE browser, Firefox browser,Google Browser has been tested , very successful, Everyone can practice.

Related Article

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.