JavaScript get_headers
fetches all the headers sent by the server in response to a HTTP request
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 35 | function get_headers (url, format) { // + original by: Paulo Freitas // + bugfixed by: Brett Zamir (http://brett-zamir.me) // % note 1: This function uses XmlHttpRequest and cannot retrieve resource from different domain. // % note 1: Synchronous so may lock up browser, mainly here for study purposes. // * example 1: get_headers('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm')[0]; // * returns 1: 'Date: Wed, 13 May 2009 23:53:11 GMT' var req = this.window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest(); if (!req) { throw new Error('XMLHttpRequest not supported'); } var tmp, headers, pair, i, j = 0; req.open('HEAD', url, false); req.send(null); if (req.readyState < 3) { return false; } tmp = req.getAllResponseHeaders(); tmp = tmp.split('\n'); tmp = this.array_filter(tmp, function (value) { return value.substring(1) !== ''; }); headers = format ? {} : []; for (i in tmp) { if (format) { pair = tmp[i].split(':'); headers[pair.splice(0, 1)] = pair.join(':').substring(1); } else { headers[j++] = tmp[i]; } } return headers; } |
Examples
Running
1 | get_headers('http://kevin.vanzonneveld.net/pj_test_supportfile_1.htm')[0]; |
Should return
1 | 'Date: Wed, 13 May 2009 23:53:11 GMT' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have get_headers goodness in JavaScript.
OMG! Sorry for the flood and ignore my last 2 comments (#7 and #8 - I was quite confused). Here's the function with corrections:
[code="javascript"]
function get_headers(url, format) {
var req = window.ActiveXObject ? new ActiveXObject("Microsoft.XMLHTTP") : new XMLHttpRequest();
if (!req) throw new Error('XMLHttpRequest not supported');
var tmp, headers, pair, i;
req.open('HEAD', url, false);
req.send(null);
if (req.readyState < 3) {
return false;
}
tmp = req.getAllResponseHeaders();alert(tmp);
tmp = tmp.split('\n');
tmp = array_filter(tmp, function (value) { return value.substring(1) != ''; });
headers = [req.status + ' ' + req.statusText];
for (i in tmp) {
if (format) {
pair = tmp[i].split(':');
headers[pair.splice(0, 1)] = pair.join(':').substring(1);
} else {
headers[headers.length] = tmp[i];
}
}
return headers;
}
[/code]
About the Opera problem, I don't know how to fix it. In my tests, Opera 9.62 returned a empty string in statusText property and getAllResponseHeaders() function - only status property returned as expected. Both Firefox 3 and IE 7 worked perfectly. :)
Ah! This line:
headers[pair.splice(0, 1)] = pair.join(':').replace(/^\s+|\s+$/g, '');
might be only this, there's no difference:
headers[pair.splice(0, 1)] = pair.join(':');
And as far I've noted, to work properly in Opera, the AJAX request should be asynchronized, as here: http://www.ceciliadassi.com/get_headers2.js. Synchronized requests returns false in the most of times. ;/
Hmm, might be useful replace
[code="javascript"]tmp.pop();[/code]
to
[code="javascript"]tmp = array_filter(tmp, function (str) { return str.replace(/^\s+|\s+$/g, '') != ''; });[/code]
to avoid any unexpected problem. ;)
Oops, sorry for the inconvenient - it's a bit buggy and I don't know how it worked before.
This snippet should fixes all the problems:
[code="javascript"] tmp = req.getAllResponseHeaders().split('\n');
tmp.pop();
headers = [req.status + ' ' + req.statusText];
for (i in tmp) {
if (format) {
pair = tmp[i].split(':');
headers[pair.splice(0, 1)] = pair.join(':').replace(/^\s+|\s+$/g, '');
} else {
headers[headers.length] = tmp[i];
}
}[/code]
Here's a temporary demo - working as expected: http://www.ceciliadassi.com/get_headers.html
[]'s
@ Paulo Ricardo F. Santos: I'm testing from console using the ./phpjstest.php file in the _tools directory in svn. But what's strange is that code from file_size (which I thought was identical) does not break.
@ Kevin: Ah! Well... how you are testing? AFAIK, there's no way to test it locally or request a cross-domain address. I've tested it on my test domain, requesting a file from same server. ;)
@ Paulo Ricardo F. Santos: Well I haven't got it working yet, so that's why the examples are still in a copy-pasted state.


Kevin van Zonneveld
10 Dec '08