Skip to content

Instantly share code, notes, and snippets.

@tincho
Last active December 13, 2018 01:58
Show Gist options
  • Save tincho/3c71467f6a2db604b5f5ecbef3174735 to your computer and use it in GitHub Desktop.
Save tincho/3c71467f6a2db604b5f5ecbef3174735 to your computer and use it in GitHub Desktop.
function getHeader(url, header) {
return new Promise(function(resolve, reject) {
var http = new XMLHttpRequest();
http.open('HEAD', url);
http.onreadystatechange = function() {
if (this.readyState !== this.DONE) return;
if (this.status != 200) {
reject(this.status);
} else {
var value = header ? this.getResponseHeader(header) : this.getAllResponseHeaders();
resolve(value);
}
};
http.send();
});
}
// usage:
getHeader('http://....', 'Content-Type').then(function(contentType) {
// HTTP OK (200)
}, function() {
// rejected because HTTP non-OK
});
getHeader('http://....').then(function(allHeaders) {
// Response.getAllResponseHeaders()
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment