掌握JavaScript的用户代理 [英] Getting the User Agent with JavaScript
本文介绍了掌握JavaScript的用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获得一个脚本,可以获取用户的用户代理,并支撑一个属性。
我在做一个网站的问题联系表,我通常需要知道用户正在使用的浏览器。如何检测用户代理字符串和支撑它作为一个输入元素的值。
我的HTML看起来像:
<输入类型=隐藏ID =的UserAgentNAME =用户代理/>
我希望用户代理被添加到作为值属性,以便它看起来像:
<输入类型=隐藏ID =的UserAgentNAME =用户代理VALUE =Mozilla的/ 5.0(Macintosh上,英特尔的Mac OS X 10_7_3)为AppleWebKit / 534.53。 11(KHTML,例如Gecko)版本/ Safari浏览器5.1.3 / 534.53.10/>
解决方案
纯JavaScript
\r
\r\r
\r 的document.getElementById('的UserAgent')值= navigator.userAgent的;
\r
<输入类型=文本ID =的UserAgent>
\r
的jQuery
\r
\r\r
\r $('#用户代理)VAL(navigator.userAgent的);
\r
&LT;脚本SRC =https://ajax.googleapis.com/ajax /libs/jquery/1.11.1/jquery.min.js\"></script>\r
\r
&LT;输入类型=文本ID =的UserAgent&GT;
\r
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" />
解决方案
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">
这篇关于掌握JavaScript的用户代理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文