Getting user agent with JavaScript

I would like to get a script that can capture the user agent of the user and assign an attribute to it.

I create a contact form for problems with the site, and I usually need to know which browser it uses. How to define a user agent string and specify it as the value of an input element.

My html looks something like this:

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

I want the user agent to be added to it as a value attribute, so that it looks like this:

 <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" /> 
+69
javascript jquery prop attributes user-agent
Feb 26 2018-12-12T00
source share
2 answers

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"> 
+149
Feb 26 '12 at 2:45
source share

The original Q said nothing about jQuery. So

 document.getElementById('UserAgent').value = navigator.userAgent; 
+43
Jun 11 '14 at 19:31
source share



All Articles