I want to capture the HTTP request header fields, primarily the Referer and User-Agent, within my client-side JavaScript. How may I access them?
我想在我的客户端JavaScript中捕获HTTP请求头字段,主要是Referer和User-Agent。我怎么能访问它们?
Google Analytics manages to get the data via JavaScript that they have you embed in you pages, so it is definitely possible.
Google Analytics(分析)设法通过JavaScript将您嵌入页面中的数据获取,因此绝对可以。
Related:
Accessing the web page's HTTP Headers in JavaScript相关:在JavaScript中访问网页的HTTP标头
6 个解决方案
#1
56
If you want to access referrer and user-agent, those are available to client-side Javascript, but not by accessing the headers directly.
如果要访问referrer和user-agent,那些可用于客户端Javascript,但不能直接访问头。
To retrieve the referrer, use document.referrer
.
To access the user-agent, use navigator.userAgent
.
要检索引用者,请使用document.referrer。要访问用户代理,请使用navigator.userAgent。
As others have indicated, the HTTP headers are not available, but you specifically asked about the referer and user-agent, which are available via Javascript.
正如其他人所指出的那样,HTTP标头不可用,但您特别询问了可通过Javascript获得的referer和user-agent。
#2
8
Almost by definition, the client-side JavaScript is not at the receiving end of a http request, so it has no headers to read. Most commonly, your JavaScript is the result of an http response. If you are trying to get the values of the http request that generated your response, you'll have to write server side code to embed those values in the JavaScript you produce.
几乎按照定义,客户端JavaScript不在http请求的接收端,因此它没有要读取的标头。最常见的是,您的JavaScript是http响应的结果。如果您尝试获取生成响应的http请求的值,则必须编写服务器端代码以将这些值嵌入到您生成的JavaScript中。
It gets a little tricky to have server-side code generate client side code, so be sure that is what you need. For instance, if you want the User-agent information, you might find it sufficient to get the various values that JavaScript provides for browser detection. Start with navigator.appName and navigator.appVersion.
让服务器端代码生成客户端代码变得有点棘手,所以请确保这就是您所需要的。例如,如果您需要用户代理信息,您可能会发现它足以获取JavaScript为浏览器检测提供的各种值。从navigator.appName和navigator.appVersion开始。
#3
6
This can be accessed through Javascript because it's a property of the loaded document, not of its parent.
这可以通过Javascript访问,因为它是加载文档的属性,而不是其父文档的属性。
Here's a quick example:
这是一个简单的例子:
<script type="text/javascript">
document.write(document.referrer);
</script>
The same thing in PHP would be:
PHP中的相同内容是:
<?php echo $_SERVER["HTTP_REFERER"]; ?>
#4
0
I would imagine Google grabs some data server-side - remember, when a page loads into your browser that has Google Analytics code within it, your browser makes a request to Google's servers; Google can obtain data in that way as well as through the JavaScript embedded in the page.
我认为Google会抓取一些数据服务器端 - 请记住,当一个页面加载到您的浏览器中并且其中包含Google Analytics代码时,您的浏览器会向Google的服务器发出请求; Google可以通过这种方式获取数据,也可以通过页面中嵌入的JavaScript获取数据。
#5
0
Referer and user-agent are request header, not response header.
Referer和user-agent是请求头,而不是响应头。
That means they are sent by browser, or your ajax call (which you can modify the value), and they are decided before you get HTTP response.
这意味着它们是通过浏览器或您的ajax调用(可以修改该值)发送的,并且在获得HTTP响应之前就会确定它们。
So basically you are not asking for a HTTP header, but a browser setting.
所以基本上你不是要求HTTP标头,而是要求浏览器设置。
The value you get from document.referer and navigator.userAgent may not be the actual header, but a setting of browser.
您从document.referer和navigator.userAgent获得的值可能不是实际的标题,而是浏览器的设置。
#6
-9
var ref = Request.ServerVariables("HTTP_REFERER");
Type within the quotes any other server variable name you want.
在引号内键入所需的任何其他服务器变量名称。
#1
56
If you want to access referrer and user-agent, those are available to client-side Javascript, but not by accessing the headers directly.
如果要访问referrer和user-agent,那些可用于客户端Javascript,但不能直接访问头。
To retrieve the referrer, use document.referrer
.
To access the user-agent, use navigator.userAgent
.
要检索引用者,请使用document.referrer。要访问用户代理,请使用navigator.userAgent。
As others have indicated, the HTTP headers are not available, but you specifically asked about the referer and user-agent, which are available via Javascript.
正如其他人所指出的那样,HTTP标头不可用,但您特别询问了可通过Javascript获得的referer和user-agent。
#2
8
Almost by definition, the client-side JavaScript is not at the receiving end of a http request, so it has no headers to read. Most commonly, your JavaScript is the result of an http response. If you are trying to get the values of the http request that generated your response, you'll have to write server side code to embed those values in the JavaScript you produce.
几乎按照定义,客户端JavaScript不在http请求的接收端,因此它没有要读取的标头。最常见的是,您的JavaScript是http响应的结果。如果您尝试获取生成响应的http请求的值,则必须编写服务器端代码以将这些值嵌入到您生成的JavaScript中。
It gets a little tricky to have server-side code generate client side code, so be sure that is what you need. For instance, if you want the User-agent information, you might find it sufficient to get the various values that JavaScript provides for browser detection. Start with navigator.appName and navigator.appVersion.
让服务器端代码生成客户端代码变得有点棘手,所以请确保这就是您所需要的。例如,如果您需要用户代理信息,您可能会发现它足以获取JavaScript为浏览器检测提供的各种值。从navigator.appName和navigator.appVersion开始。
#3
6
This can be accessed through Javascript because it's a property of the loaded document, not of its parent.
这可以通过Javascript访问,因为它是加载文档的属性,而不是其父文档的属性。
Here's a quick example:
这是一个简单的例子:
<script type="text/javascript">
document.write(document.referrer);
</script>
The same thing in PHP would be:
PHP中的相同内容是:
<?php echo $_SERVER["HTTP_REFERER"]; ?>
#4
0
I would imagine Google grabs some data server-side - remember, when a page loads into your browser that has Google Analytics code within it, your browser makes a request to Google's servers; Google can obtain data in that way as well as through the JavaScript embedded in the page.
我认为Google会抓取一些数据服务器端 - 请记住,当一个页面加载到您的浏览器中并且其中包含Google Analytics代码时,您的浏览器会向Google的服务器发出请求; Google可以通过这种方式获取数据,也可以通过页面中嵌入的JavaScript获取数据。
#5
0
Referer and user-agent are request header, not response header.
Referer和user-agent是请求头,而不是响应头。
That means they are sent by browser, or your ajax call (which you can modify the value), and they are decided before you get HTTP response.
这意味着它们是通过浏览器或您的ajax调用(可以修改该值)发送的,并且在获得HTTP响应之前就会确定它们。
So basically you are not asking for a HTTP header, but a browser setting.
所以基本上你不是要求HTTP标头,而是要求浏览器设置。
The value you get from document.referer and navigator.userAgent may not be the actual header, but a setting of browser.
您从document.referer和navigator.userAgent获得的值可能不是实际的标题,而是浏览器的设置。
#6
-9
var ref = Request.ServerVariables("HTTP_REFERER");
Type within the quotes any other server variable name you want.
在引号内键入所需的任何其他服务器变量名称。