Javascript Image - access headers in response

2020-02-14 04:20发布

问题:

When changing the .src property of a Javascript Image object, a request is send to the specified 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?

回答1:

No, there is not. The headers would only be available to you if you downloaded the image yourself with your own ajax call. The browser doesn't make the image headers available when it automatically does the downloading via the .src property.

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.