如何通过JavaScript访问HTTP请求标头字段?

问题描述:

我想在我的客户端JavaScript中捕获HTTP请求头字段,主要是Referer和User-Agent。我如何访问它们?

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?

Google Analytics设法通过JavaScript将您嵌入页面中的数据获取,所以它绝对有可能。

Google Analytics manages to get the data via JavaScript that they have you embed in you pages, so it is definitely possible.


相关:

使用JavaScript访问网页的HTTP标头


如果您想访问referrer和user-agent,那些可用于客户端Javascript,但不能直接访问标题。

If you want to access referrer and user-agent, those are available to client-side Javascript, but not by accessing the headers directly.

要检索推荐人,请使用 document.referrer

要访问用户代理,请使用 navigator.userAgent

To retrieve the referrer, use document.referrer.
To access the user-agent, use navigator.userAgent.

其他人有籼稻ted,HTTP标头不可用,但你明确询问了referer和user-agent,它们可以通过Javascript获得。

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.