0

I'm trying to get this javascript code to display simply the name of the browser I am using to display the web page. I am using safari and none of the navigators display the fact that I am running safari. On my browser, it displays the following:

Name: Netscape Code name: Mozilla Product: Gecko Version: 5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8 Platform: MacIntel User agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_3) AppleWebKit/602.4.8 (KHTML, like Gecko) Version/10.0.3 Safari/602.4.8

How can I get the script to display what browser the client is using? I've read from other posts that navigator.appName achieves this, but it doesn't since Netscape is not the name of the browser; Safari in my case is.

function BrowserIdentity() {
 document.getElementById("browser-detection").innerHTML = "Name: " + navigator.appName + "<br>Code name: " + navigator.appCodeName +"<br>Product: " + navigator.product + "<br>Version: " + navigator.appVersion + "<br>Platform: " + navigator.platform + "<br>User agent: " + navigator.userAgent;
}
<h3>Navigator Details</h3>

<p>Click the button to display navigator details:</p>

<button onClick="BrowserIdentity()">Click Me</button>

<p id="browser-detection"></p>
user3760941
  • 518
  • 5
  • 19
  • Possible duplicate of [Why does JavaScript navigator.appName return Netscape for Safari, Firefox and Chrome?](http://stackoverflow.com/questions/14573881/why-does-javascript-navigator-appname-return-netscape-for-safari-firefox-and-ch) – Andreas Mar 26 '17 at 15:57
  • [`navigator.appName`](https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/appName): _"This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Avoid using it and update existing code if possible;"_ – Andreas Mar 26 '17 at 15:58
  • interesting comments. is it not possible to achieve browser detection anymore then? – user3760941 Mar 26 '17 at 16:03
  • Out of curiosity why are you performing browser detection? A much more robust solution would be to perform feature detection and gracefully degrade your app should the browser not support a required feature. – ste2425 Mar 26 '17 at 16:37
  • it's a requirement for a javascript course I'm doing – user3760941 Mar 26 '17 at 17:09

1 Answers1

0

Use navigator.userAgent, then parse the returned value.

Tony Duffill
  • 277
  • 1
  • 5