使用 JavaScript 修改 HTTP 标头客户端

发布于 2024-08-01 22:32:55 字数 126 浏览 3 评论 0原文

是否可以使用 JavaScript 动态更改从外部源加载图像时收到的 HTTP 标头? 我试图在客户端控制图像的缓存(ExpiresMax-Age 等...),因为我无权访问服务器。

Is it possible to use JavaScript to dynamically change the HTTP Headers received when loading an image from an external source? 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

尝蛊 2024-08-08 22:32:55

正如其他人所说,不,不可能在客户端代码中操纵来自服务器的 http 标头和缓存指令。

可能发生的事情

您有能力做的是确保您获得新文件。 这可以通过将唯一字符串作为查询字符串参数附加到请求的 URL 来完成。

例如,如果您想确保每小时都有一个新文件,

<script type="text/javascript">

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

</script>

那么它的作用就是在 url 中添加一个包含年、日和小时的值,这样每个小时它都是唯一的,从而确保新的文件请求。 (未经测试!)

显然这可以变得更加通用和微调,但希望您能明白这个想法。

不可能的事情

您不能做的是确保您不会从服务器检索新版本。

As the others have said, no, it is not possibly to manipulate http headers and caching directives from the server in client code.

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.

e.g. 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. (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.

就此别过 2024-08-08 22:32:55

缓存指令由服务器负责。 您无法在客户端操纵它们。

也许您可以选择安装代理服务器,例如,如果您的目标是公司员工?

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, e.g. if you are aiming at company employees?

猛虎独行 2024-08-08 22:32:55

我不认为 Javascript 实际上可以做到这一点:浏览器请求图像,并且由浏览器定义要发出的 HTTP 标头。

使用某些自定义标头的一种方法是使用某种 Ajax 请求,而不传递任何 标记; 但你必须知道如何处理返回的数据......不认为这有多大帮助。

如果您希望浏览器将图像保存在缓存中,则服务器必须在响应中发送正确的标头(例如 Etag 和/或过期 - 请参阅 mod_expires< /a>,例如,对于 Apache)

如果您想绝对确定浏览器将下载新图像,而不是使用缓存中的版本,则应每次使用不同的 URL。

这通常是使用时间戳作为 URL 的参数来完成的; 像 example.com/image.jpg?123456789 (123456789 或多或少是当前时间戳 - 显然小于更多,但你明白了:每一秒,浏览器都会看到 URL 发生了变化)< /em>

编辑问题后进行编辑:

Expires 标头由服务器生成,是响应中出现的标头之一(它不是客户端在请求中发送的标头;请参阅 HTTP 标头列表)。

因此,您绝对无法从客户端控制它:必须配置服务器来完成这项工作,在这里......

如果您想要更多答案:您到底想做什么? 为什么 ?

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.

One way to use some custom headers would be with some kind of Ajax-request, not passing by any <img> tag ; 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)

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.

This is often done using the timestamp as a parameter to the 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)

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).

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 ?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文