使用 JavaScript 获取用户代理 [英] Getting the User Agent with JavaScript

查看:56
本文介绍了使用 JavaScript 获取用户代理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个脚本,它可以获取用户的用户代理并将其支持到一个属性.

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.

我的 html 看起来像:

My html looks something like:

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

我希望将用户代理作为 value 属性添加到其中,使其看起来像:

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">

$('#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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆