Getting the User Agent with JavaScript

JavascriptJqueryAttributesUser AgentProp

Javascript Problem Overview


I'd like to get a script that can grab the user's user agent and prop it to an attribute.

I'm making a website problems contact form and I usually need to know what browser the user is using. How can I detect the user agent string and prop it as the value of an input element.

My html looks something like:

<input type="hidden" id="UserAgent" name="User Agent" />

I want the user agent to be added to that as the value attribute so it would look like:

<input type="hidden" id="UserAgent" name="User Agent" value="Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_3) AppleWebKit/534.53.11 (KHTML, like Gecko) Version/5.1.3 Safari/534.53.10" />

Javascript Solutions


Solution 1 - Javascript

Pure Javascript

document.getElementById('UserAgent').value = navigator.userAgent;

<input type="text" id="UserAgent">

jQuery

$('#UserAgent').val(navigator.userAgent);

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<input type="text" id="UserAgent">

Solution 2 - Javascript

Original Q didn't say anything about jQuery. so

document.getElementById('UserAgent').value = navigator.userAgent;

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhenryaaronView Question on Stackoverflow
Solution 1 - JavascriptAdam MerrifieldView Answer on Stackoverflow
Solution 2 - JavascriptaxlotlView Answer on Stackoverflow