views:

1685

answers:

3

Is it possible to do a HTTP Head request solely using an XMLHTTPRequest in JavaScript?

My motivation is to conserve bandwidth.

If not, is it possible to fake it?

+6  A: 

Easy, just use the HEAD method, instead of GET or POST:

function UrlExists(url)
{
  var http = new XMLHttpRequest();
  http.open('HEAD', url, false);
  http.send();
  return http.status!=404;
}
doekman
Thanks, sometimes the abstraction of a framework hides the underlying functionality!
EoghanM
Any idea how cross-browser this is? The jQuery documentation states "Note: Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers." http://api.jquery.com/jQuery.ajax/
Bobby Jack
+1  A: 

An XMLHTTPRequest object should have

getAllResponseHeaders();
getResponseHeader("header-name")

defined on it

adam
A: 

Yes, check this...

coder