jQuery plugin: ajax Head Request
January 21, 2009 in jQuery Plugins
I recently looked into the head request for pulling data without much overhead. PHP can send headers out to the browser in a response.. so I looked into a way to get these headers. I came across a page that outlined using head requests via the xmlhttprequest object.
I looked inside jQuery for this functionality and noticed it wasn’t there so I copied the $.post functionality and modified it into $.head.
So I present to you my Plugin for jQuery
/* jQuery.head - v1.0.3 - K Reeve aka BinaryKitten
*
* makes a Head Request via XMLHttpRequest (ajax) and returns an object/array of headers returned from the server
* $.head(url, [data], [callback])
* url The url to which to place the head request
* data (optional) any data you wish to pass - see $.post and $.get for more info
* callback (optional) Function to call when the head request is complete.
* This function will be passed an object containing the headers with
* the object consisting of key/value pairs where the key is the header name and the
* value it's corresponding value
*
* for discussion and info please visit: http://binarykitten.me.uk/jQHead
*
* ------------ Version History -----------------------------------
* v1.0.3
* Fixed the zero-index issue with the for loop for the headers
* v1.0.2
* placed the function inside an enclosure
*
* v1.0.1
* The 1st version - based on $.post/$.get
*/
(function ($) {
$.extend({
head: function( url, data, callback ) {
if ( $.isFunction( data ) ) {
callback = data;
data = {};
}
return $.ajax({
type: "HEAD",
url: url,
data: data,
complete: function (XMLHttpRequest, textStatus) {
var headers = XMLHttpRequest.getAllResponseHeaders().split("\n");
var new_headers = {};
var l = headers.length;
for (var key=0;key<l;key++) {
if (headers[key].length != 0) {
header = headers[key].split(": ");
new_headers[header[0]] = header[1];
}
}
if ($.isFunction(callback)) {
callback(new_headers);
}
}
});
}
});
})(jQuery);
The function calls the passed url, passing the data and then processes the headers on completion. It takes the long text passed and splits it up into each header line and then splits it into array. The passed array is sent back to the callback function where the key of the array is the header name and the value being the header value.
To call our new function, we use the form just like the basic functionality of $.post and $.get:
/* $.head("url",{data},callback_function(headers) { }); */
/* Example */
$.head("index.php",{'a':5,'bc':'help'},function(headers) {
$.each(headers,function(key,header){ console.log(key+':--:'+header);});
});
This will use firebug’s console.log to output the details of each header.
all straight forward yes?
Hopefully this will help or do something for the people out there
Enjoy
< Edit >
This has now been placed on plugins.jquery.com -> http://plugins.jquery.com/project/jqHead
[...] just added a jquery plugin to call head requests, but because you are not doing a get or post request php won’t convert [...]
This is short, succinct, and insanely useful – as a jQuery plugin should be! Thanks for sharing. And I don’t know how I overlooked the HEAD method. I can definitely see that coming in useful for effciently polling server-side resources.
No problems, I actually use it myself for status checks.. since it’s technically less bandwidth.
this should be added to the core .. really.
damn that’s what i was looking for some weeks ago -.-
well, next time then ^^
thanks a lot – great job !
Was this made into a downloadable/recyclable plugin yet? I can’t find it in the jQuery plugin repository. It would be great to see it there.
yes.. i have done so now… please visit -> http://plugins.jquery.com/project/jqHead
I would note that in the source, you may wish to change your for loop from a “for…in” to a simple for loop.
When I use the current code, it comes back with an undefined error for split because one of the keys points to an exists() function. (which may be being defined against the Array Object by one of my libraries). Being a function, doesn’t have a split() function.
Alternatively, you could test to make sure that what you are working against is a string. Either way will help to increase robustness.
You may find the following interesting: http://stackoverflow.com/questions/500504/javascript-for-in-with-arrays/500531
Cgp, thanks for that .. have updated.
I had to change
for (var key=0;key<=l;key++) {
to
for (var key=0;key<l;key++) {
to get this to work. Which makes sense now I think about it…
Bug in code posted at: http://plugins.jquery.com/files/jquery.ajax_head.js.txt
Line “for (var key=0;key<=l;key++) {"
should be "for (var key=0;key < l;key++) {"
Yay for 0 indexes.
Hi,
I like your plug-in but missed the “status” header from the Response. Doing some debugging in Firefox, I found that you can actually access the “Status” response from a webserver in the XMLHttpRequest object in javascript. So I have created “a new version” of your plug-in which adds status and two other response headers to the “header” array returned to the callback function. It is the “multipart” and “withCredentials” headers.
Also I have created a new method “exists(url, callback)” which will return a true|false value to the callback function. The url could be for instance a local MP3 file.
You can find my “new sub version” here: http://dl.dropbox.com/u/3260327/jQuery.head.js
Thanks.
hi; nice script !
im new to jquery . I have downloaded the new version and would like to know how to use it ?
thanks
jeff
you use this the same as you would $.post or $.get headers are passed to the callback. This does not make use of promises or deferred as it predates them by quite a lot.