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:
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.