How can Javascript view the ETAG value of a HTTP response? Can ETAG be included in a request?

halfbit picture halfbit · Mar 11, 2012 · Viewed 7.2k times · Source

Is it possible for javascript to read the ETAG value of a HTTP response?

Are there any issues in writing out an ETAG value in a HTTP Request to the server?

Answer

Waynn Lue picture Waynn Lue · Mar 11, 2012

If it's a regularly loaded page, I think the HTTP request/response is already complete so Javascript can't access that information.

If it's an AJAX call, jQuery has the jqXHR object (documentation here), which says this.

The jQuery XMLHttpRequest (jqXHR) object returned by $.ajax() as of jQuery 1.5 is a superset of the browser's native XMLHttpRequest object. For example, it contains responseText and responseXML properties, as well as a getResponseHeader() method. When the transport mechanism is something other than XMLHttpRequest (for example, a script tag for a JSONP request) the jqXHR object simulates native XHR functionality where possible.

So you can call getResponseHeader() on the returned object in your success call to parse them out.