Get the types & versions of Browsers via JavaScript

The code for getting the types and versions of Browsers by using JavaScript:
<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;

//Doing 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>

Packing the above coding into a function, returns Object Sys, which includes browser’s type and version detail, as shown below:
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;
}

While using this function, it could call ‘getBrowserInfo’, as shown below:
<script type=”text/javascript”>
//get the current browser’s info
var sys = getBrowserInfo();
//sys.browser is getting the browser’s type,sys.ver is getting the       browser’s version
document.write(sys.browser + “‘s version is :” + sys.ver);
</script>
The entire example as shown below:
<!DOCTYPE HTML>
<html>
<head>
<title>Getting the browser’s type and version via JavaScript</title>
<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;

//Doing 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>
<script type=”text/javascript”>
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;
}
document.write(‘<hr/>’);
//Getting the current browser’s info
var sys = getBrowserInfo();
//sys.browser is getting browser’s type,sys.ver is getting browser’s version
document.write(sys.browser + “‘s version is:” + sys.ver);
</script>
</head>
<body>
</body>
</html>


3 thoughts on “Get the types & versions of Browsers via JavaScript”

Leave a Reply