Skip to content

Instantly share code, notes, and snippets.

@phiggins42
Created August 9, 2010 21:17
Show Gist options
  • Save phiggins42/516137 to your computer and use it in GitHub Desktop.
Save phiggins42/516137 to your computer and use it in GitHub Desktop.
<!DOCTYPE html>
<html>
<head>
<title> Testing dojo.xhr HEAD </title>
<script src="http://ajax.googleapis.com/ajax/libs/dojo/1.5/dojo/dojo.xd.js"></script>
<script>
dojo.ready(function(){
var d = dojo, handlers = d.contentHandlers;
handlers.size = function(xhr){
// summary: a handleAs handler for getting file size or not
var size = xhr.getAllResponseHeaders().match(/content-length: (.*)/i);
return size && size[1];
};
handlers.mixed = function(xhr){
// summary: a handleAs handler for returning both filesize and original handleAs:"text" in a mixed obj
return {
filesize: handlers.size(xhr),
html: handlers.text(xhr) // can we infer file is in cache if HEAD is populated? happens in FF3+ sofar.
};
};
var doit = function(){
// summary: do a HEAD query on a url
// returns: dojo.Deferred
return d.xhr("HEAD", {
url: "big.jpg",
handleAs:"mixed",
load: function(info){
/* clearly we exist */
console.log("file info", info);
if(info.html && d.trim(info.html).length){
console.log("it seems the url was cached?", info.html.length);
}
},
error: function(e){
console.warn(e);
/* something went wrong */
}
});
}
// do our HEAD query, then place an image in the dom. onload, check the HEAD response again.
doit().then(function(){
var n = d.place("<img src='big.jpg'>", d.body());
d.connect(n, "onload", doit);
});
});
</script>
</head>
<body></body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment