2

I'm trying to write a simple script that will display the current browser name, version, and the operating system. Here's but don't think I'm on the right path. Also how can I use parseInt() function to print just the version number?

<!DOCTYPE html>
<html>
<head>
    <title>Window Open Example 2</title>
</head>
<body>
     <script type="text/javascript">
        document.write(navigator.appVersion);
     </script>
</body>
</html>
Marius Bancila
  • 15,287
  • 7
  • 44
  • 84
  • 1
    possible duplicate of [Detect version of browser](http://stackoverflow.com/questions/5916900/detect-version-of-browser) – Nirbhay Kundan Aug 20 '14 at 08:00
  • 1
    Standards do not support browser sniffing instead they support determining if the browser has a feature. What this means is that whatever you do today to determine the browser may not work for the next version of browser. – Wayne Aug 20 '14 at 08:01
  • 1
    Refer `http://stackoverflow.com/questions/9514179/how-to-find-the-operating-system-version-using-javascript` – Neeraj Dubey Aug 20 '14 at 08:01
  • Feature detection is a more modern approach to this. – Mark Walters Aug 20 '14 at 08:06

2 Answers2

2

You should use parseFloat in case of Version number like "5.01"

document.write(parseFloat(navigator.appVersion).toFixed(1));

toFixed() tells how many dezimal places should be visible.

Paul Bönisch
  • 284
  • 2
  • 10
0

for get browser name

<script>document.getElementById("demo").innerHTML
="Name is " + navigator.appName +". ode name is " + navigator.appCodeName;

for version

document.getElementById("demo").innerHTML = navigator.userAgent;

for OS

var OSName="unknown OS";
if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
document.write('Your OS: '+OSName);

for parseINT

      document.getElementById("demo").innerHTML =
      parseInt(navigator.appVersion, 10);
yugi
  • 732
  • 13
  • 25
  • 1
    -1 `navigator.appName` does not return the correct browser name since all browsers except IE will probably return `Netscape` instead. `userAgent` returns versions for every part of the browser. – Derek 朕會功夫 Aug 20 '14 at 08:13
  • yes.but reason for that [click here](http://stackoverflow.com/questions/14573881/why-the-javascript-navigator-appname-returns-netscape-for-safari-firefox-and-ch) – yugi Aug 20 '14 at 10:00
  • I know why browsers are returning "Netscape". What basically I'm saying is that your answer is wrong. – Derek 朕會功夫 Aug 20 '14 at 19:19