Javascript Reference - Navigator userAgent Property








The userAgent property returns the value of the user-agent header sent by the browser to the server.

The value contains information about the name, version and platform of the browser.

Browser Support

userAgent Yes Yes Yes Yes Yes

Syntax

var v = navigator.userAgent

Return Value

Type Description
String The user agent string for the current browser




Example

A demonstration of all navigator properties.


<!DOCTYPE html>
<html>
<body>
<!--from w w w.j a va  2s . c o  m-->
<div id="demo"></div>

<script>
txt = "<p>Browser CodeName: " + navigator.appCodeName + "</p>";
txt+= "<p>Browser Name: " + navigator.appName + "</p>";
txt+= "<p>Browser Version: " + navigator.appVersion + "</p>";
txt+= "<p>Cookies Enabled: " + navigator.cookieEnabled + "</p>";
txt+= "<p>Browser Language: " + navigator.language + "</p>";
txt+= "<p>Browser Online: " + navigator.onLine + "</p>";
txt+= "<p>Platform: " + navigator.platform + "</p>";
txt+= "<p>User-agent header: " + navigator.userAgent + "</p>";
txt+= "<p>User-agent language: " + navigator.systemLanguage + "</p>";

document.getElementById("demo").innerHTML = txt;
</script>

</body>
</html>

The code above is rendered as follows:





Example 2

The following code shows how to get the user-agent header sent by your browser.


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--   w  w w.jav a 2s . c o m-->
    var x = "User-agent header sent: " + navigator.userAgent;
    document.getElementById("demo").innerHTML = x;
}
</script>

</body>
</html>

The code above is rendered as follows: