简体   繁体   English

Javascript Image - 访问响应头

[英]Javascript Image - access headers in response

When changing the .src property of a Javascript Image object, a request is send to the specified URL.更改 Javascript Image 对象的 .src 属性时,会向指定的 URL 发送请求。

// new image object    
var imgObject = new Image();

// assign the path to the image to the src property   
imgObject.src = 'http://example.com/' + 'image/image.jpg';

The server's response contains headers like any other response.服务器的响应包含与任何其他响应一样的标头。 My question is: is there a way to access this headers in the onready, onload or onreadystatechange events of the Image objects once the server sent the response?我的问题是:一旦服务器发送响应,有没有办法在 Image 对象的 onready、onload 或 onreadystatechange 事件中访问此标头?

No, there is not.不,那里没有。 The headers would only be available to you if you downloaded the image yourself with your own ajax call.只有当您使用自己的 ajax 调用自己下载图像时,才能使用标头。 The browser doesn't make the image headers available when it automatically does the downloading via the .src property.当浏览器通过.src属性自动进行下载时,它不会使图像标题可用。

You may also want to keep in mind that the browser likes to cache images in the browser cache and when loaded from there, they wouldn't have headers either.您可能还想记住,浏览器喜欢在浏览器缓存中缓存图像,并且从那里加载时,它们也不会有标题。

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

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