JavaScript browser browser type and version judgment code _javascript tips

Source: Internet
Author: User
Return (Array) borwser
For example, the current browser type is ie6.0, then the Borwser value should be ["ie", "6", "6.0"]
The first value of the array represents the type of browser, the second value represents a large version of the browser, and the third value represents the detailed version number of the browser.
Copy Code code as follows:

/**
* @author Sky
*/
var browser = function ()
{
Default is IE6
var _a = ["ie", "6", "6.0"];
var _s = false;
var _ua = Navigator.userAgent.toLowerCase ();
Detect regular
var _pat = {
Opera:/opera. ([\d.] +)/,
ie:/msie ([\d.] +)/,
FF:/firefox\/([\d.] +)/,
Chrome:/chrome\/([\d.] +)/,
Safari:/safari\/([\d.] +)/,
Mozilla:/rv: ([\d.] +). +gecko/
};
for (Var _t in _pat)
{
var _s = _ua.match (_pat[_t]);
if (_s)
{
_a = [_t, parseint (_s[1]), _s[1]];
Break
}
}
return _a;
}(),

Simple Demo: The above code should be one of several methods in the framework
<script> var browser = function () {//default = IE6 var _a = ["ie", "6", "6.0"]; var _s = false; var _ua = Navigator.userAgent.toLowerCase (); Detect regular var _pat = {opera:/opera. ( [\d.] +)/, ie:/msie ([\d.] +)/, FF:/firefox\/([\d.] +)/, Chrome:/chrome\/([\d.] +)/, Safari:/safari\/([\d.] +)/, Mozilla:/rv: ([\d.] +). +gecko/}; for (Var _t in _pat) {var _s = _ua.match (_pat[_t)); if (_s) {_a = [_t, parseint (_s[1)), _s[1]]; Break } return _a; } () var dd =browser; Alert (dd[0]+ "" +dd[1]+ "" +dd[2]); </script>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]
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.