Home > Web Front-end > JS Tutorial > body text

JavaScript browser browser type and version judgment code_javascript skills

WBOY
Release: 2016-05-16 18:29:15
Original
1381 people have browsed it

return (Array) borwser
For example, the current browser type is ie6.0, then the value of borwser should be ["ie", "6", "6.0"]
The first value of the array represents the browser Type, the second value represents the large version of the browser, and the third value represents the detailed version number of the browser.

Copy code The code is as follows:

/**
* @author sky
*/
var browser = function()
{
//The default is ie6
var _a = ["ie", "6", "6.0"];
var _s = false;
var _ua = navigator.userAgent.toLowerCase();
//Detection regularity
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 demonstration: the above code should be one of the multiple methods in the framework

[Ctrl A select all Note: If you need to introduce external Js, you need to refresh to execute
]<script> var browser = function() { //默认为ie6 var _a = ["ie", "6", "6.0"]; var _s = false; var _ua = navigator.userAgent.toLowerCase(); //检测正则 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>
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template