Page 1 of 1

Javascript get browser name and version

Posted: 05 Apr 2016, 23:32
by dmgatwork
I am looking for a JavaScript that will capture the browser name and version that is understandable. I want to determine if my viewers have older IE or a browser that will not support some of the new ideas I am thinking about adding. Thanks for any help.

Javascript get browser name and version

Posted: 06 Apr 2016, 08:20
by Admin
Hi
The navigator.userAgent property contains info about browser name and version.
You can use the browserCheck() function from this example to get the browser name and version in javascript:

Code: Select all

Click to <button id="btn_brw">Get Browser Name-Version</button>
<script>
//return an object {browser: "name", version: nr}
function browserCheck(){
  var N= navigator.appName, ua= navigator.userAgent, tem;
  var re= ua.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
  if(re && (tem= ua.match(/version\/([\.\d]+)/i))!= null) {re[2]=tem[1];}
  re= re? [re[1], re[2]]: [N, navigator.appVersion,'-?'];
  return {browser:re[0], version:parseFloat(re[1])};
}

//when click on #btn_brw, get and alert browser name and version
if(document.getElementById('btn_brw')){
  document.getElementById('btn_brw').addEventListener('click', function(){
    var brw_nv = browserCheck();
    alert('Browser: '+ brw_nv.browser +'\nVersion: '+ brw_nv.version);
  });
}
</script>
Demo:
Click to

Javascript get browser name and version

Posted: 06 Apr 2016, 19:47
by dmgatwork
This also works great, thanks. I checked to see if the ip address was available as I had not hear of navigator but it wasn't or I could not find it.

Javascript get browser name and version

Posted: 07 Apr 2016, 07:34
by Admin
From what I know, you cannot get the user's ip with javascript unless you use ajax with a server side script or some kind of external service.