0

I am using javascript code shown below for displaying my computer browser as just "Firefox 19.0".

<script language="javascript" type="text/javascript">
function detect()
{
 document.getElementById("bCodeName").innerHTML = navigator.appCodeName;
}
</script>
<div id="bCodeName"></div>
<body onload="detect();"></body>

But this code is just displaying:

Mozilla

I have no idea how to go ahead.

Please help. Thanks in advance.

user2201935
  • 376
  • 1
  • 5
  • 18
  • Open a `console` (Firebug or Chrome Console`) and type `Navigator` into the immediates jor command line. – Jared Farrish Mar 23 '13 at 09:49
  • why has this question been downvoted? – imulsion Mar 23 '13 at 09:50
  • 2
    @imulsion. I didn't downvote, but I must say it shows little research effort. If one property doesn't return exactly what you want, you should be able to look up other properties of the same object. – GolezTrol Mar 23 '13 at 09:53

3 Answers3

0

Use navigator.appName instead. I'm not sure if that returns the right name (it returns 'Netscape' in Chrome, for some reason. navigator also has some other properties like appVersion that you can use.

GolezTrol
  • 109,399
  • 12
  • 170
  • 196
0

As answered by kennebec in Browser detection in JavaScript?

navigator.sayswho= (function(){
  var N= navigator.appName, ua= navigator.userAgent, tem;
  var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
  if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
  M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
  return M;
 })();

You may also use navigator.userAgent

Here is a complete Documentation:

http://www.w3schools.com/jsref/prop_nav_useragent.asp

Community
  • 1
  • 1
0

As previously stated, you may browser through the "navigator" object properties and then properly parse the ones interesting for you (for example: "appCodeName", "appVersion" and the omnipresent "userAgent").

Two ways of doing so might be:

function detect()
{
    document.getElementById("bCodeName").innerHTML = navigator.appCodeName + " " + navigator.appVersion.split(" ")[0];
}

or a bit shorter:

function detect()
{
    document.getElementById("bCodeName").innerHTML = navigator.userAgent.split(" ")[0].replace("/", " ");
}

You may do well handling exceptions, in case there was some error whilst parsing this data. See Javascript Try/Catch

Edit:

If you want to show the specific application name and version, take a look to this great SO answer. With this, the final code would be:

function detect()
{
    var N= navigator.appName, ua= navigator.userAgent, tem;
    var M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i);
    if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
    M = M? [M[1], M[2]]: [N, navigator.appVersion, '-?'];
    document.getElementById("bCodeName").innerHTML = M[0] + " " + M[1]
}
Community
  • 1
  • 1
Kiddo
  • 151
  • 1
  • 4
  • 12
  • Hi Shere...Thanks for that but it displays Mozilla 5.0 but I need Firefox 5.0. How to go ahead with this? – user2201935 Mar 23 '13 at 11:03
  • The answer has been just updated with the output you are looking for. Play a bit with this and see if this is what you were looking for. By the way, I think it would be better that "detect()" returned the version and you inserted this into the HTML outside -- this will make your code more usable in the future. – Kiddo Mar 23 '13 at 11:34