jQuery plugin: ajax Head Request

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

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:

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

< Edit >
This has now been placed on plugins.jquery.com -> http://plugins.jquery.com/project/jqHead

14 thoughts on “jQuery plugin: ajax Head Request

  1. 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.

  2. 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.

  3. 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

  4. 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


  5. 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.

Comments are closed.