简体   繁体   English

使用JavaScript在客户端修改HTTP标头

[英]Modifying the HTTP headers client-side with JavaScript

Is it possible to use JavaScript to dynamically change the HTTP Headers received when loading an image from an external source? 从外部源加载图像时,是否可以使用JavaScript动态更改收到的HTTP标头? I'm trying to control the caching of the image ( Expires , Max-Age , etc...) client-side since I do not have access to the server. 我正在尝试控制客户端的图像缓存( ExpiresMax-Age等),因为我无法访问服务器。

As the others have said, no, it is not possibly to manipulate http headers and caching directives from the server in client code. 正如其他人所说,不,不可能在客户端代码中从服务器操作HTTP标头和缓存指令。

What is possible 有什么可能

What you do have the ability to do is ensure you get a new file. 您要做的就是确保获得新文件。 This can be done by appending a unique string to the URL of the request as a query string parameter. 这可以通过将唯一字符串作为查询字符串参数附加到请求的URL来完成。

eg if you wanted to ensure you got a new file each hour 例如,如果您想确保每个小时都有一个新文件

<script type="text/javascript">

var d = new Date();
url += ("?" +d.getYear() + "_" + d.getDay() + "_" + d.getHours());

</script>

What this does is add a value containing the year, day and hour to the url, so it will be unique for each hour, hence ensuring a new file request. 这样做是为URL添加一个包含年,日和小时的值,因此该值对于每个小时都是唯一的,从而确保了新文件的请求。 (Not tested!) (未经测试!)

Obviously this can be made much more generic and fine tuned, but hopefully you'll get the idea. 显然,可以使它更加通用和微调,但希望您能理解。

What is impossible 不可能的事

What you can't do is ensure you will not retrieve a new version from the server. 您不能做的是确保不会从服务器检索新版本。

Caching directives are in the server's responsibility. 缓存指令由服务器负责。 You can't manipulate them on the client side. 您不能在客户端上操作它们。

Maybe it's an option for you to install a proxy server, eg if you are aiming at company employees? 也许您可以选择安装代理服务器,例如,如果您针对公司员工?

I do not think Javascript can actually do that : the images are requested by the browser, and it's up to him to define HTTP-headers to issue. 我认为Javascript实际上并不能做到这一点:浏览器会请求图像,并且要由他来定义要发出的HTTP标头。

One way to use some custom headers would be with some kind of Ajax-request, not passing by any <img> tag ; 使用某些自定义标头的一种方法是使用某种Ajax请求,而不传递任何<img>标记; but you'd have to know what to do with the returned data... Don't think it would help much. 但您必须知道如何处理返回的数据...认为不会有太大帮助。

If you want your images to be kept in cache by the browser, you server has to send the right headers in the responses (like Etag , and/or Expires -- see mod_expires , for Apache, for instance) 如果希望浏览器将图像保留在缓存中,则服务器必须在响应中发送正确的标头(例如Etag和/或Expires,例如,请参阅mod_expires ,适用于Apache)

If you want to be absolutly sure the browser will download a new image, and not use the version it has in cache, you should use a different URL each time. 如果要绝对确保浏览器将下载新图像,而不使用其在缓存中具有的版本,则每次应使用不同的URL。
This is often done using the timestamp as a parameter to the URL ; 通常使用时间戳作为URL的参数来完成此操作; like example.com/image.jpg?123456789 (123456789 being, more or less, the current timestamp -- obviously less than more, but you get the idea : each second, the browser will see the URL has changed) 例如example.com/image.jpg?123456789(123456789 或多或少是当前时间戳记-明显少于当前时间戳记,但您知道了:每秒,浏览器都会看到URL更改了)


EDIT after the edit of the question : 编辑问题后进行编辑:

The Expires header is generated by the server, and is one of the headers that come in the Response (it's not a header the client sends in the Request ; see List of HTTP headers ). Expires标头是由服务器生成的,并且是响应中的标头之一(它不是客户端在Request中发送的标头;请参阅HTTP标头列表 )。

So, you absolutly have no control over it from the client-side : it's the server that must be configured to do the work, here... 因此,您绝对无法从客户端对其进行控制:必须配置服务器来完成工作,在这里...


If you want more answers : what are you trying to do exactly ? 如果您需要更多答案:您到底想做什么? Why ? 为什么呢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM