JS gets the current browser name and version number implementation method _javascript tips

Source: Internet
Author: User

Work needs to go through JS to get the name of the browser currently in use and version number, on the internet a lot of data have a key word is Navigator.appname, but this method gets the name of the browser is only two, ie or netscap, it can be used to determine whether the use of IE, but I want to get specific browser product name such as Firefox,chrome. So we have to go through navigator.useragent, but this string is very long, it's a good way to analyze his features and solve the problem with regular expressions.

(1) Get Browser name + version string

function Getbrowserinfo ()
{
var agent = Navigator.userAgent.toLowerCase ();

var regstr_ie =/msie [\d.] +;/gi;
var regstr_ff =/firefox\/[\d.] +/gi
var regstr_chrome =/chrome\/[\d.] +/gi;
var Regstr_saf =/safari\/[\d.] +/gi;
IE
if (Agent.indexof ("MSIE") > 0)
{return
agent.match (regstr_ie);
}

Firefox
if (Agent.indexof ("Firefox") > 0)
{return
agent.match (REGSTR_FF);
}

Safari
if (Agent.indexof ("Safari") > 0 && agent.indexof ("Chrome") < 0)
{
return Agent.match (REGSTR_SAF);
}

Chrome
if (Agent.indexof ("Chrome") > 0)
{return
agent.match (regstr_chrome);
}

}

(2) Then get the version number

var browser = Getbrowserinfo ();
alert (browser); 
var verinfo = (browser+ ""). Replace (/[^0-9.] /ig, ""); 

The above JS get the current use of browser name and version number implementation is small series to share all the content, hope to give you a reference, but also hope that we support cloud habitat community.

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.