function hdrDetails(i, elm, cl) {
cl = cl/1024;
var sz = cl>1024?"MB":"KB";
cl = cl>1024?cl/1024:cl;
var len = $(elm).eq(i).text().length;
if(len > 0) {
$(elm).eq(i).append(" ("+cl.toFixed(0)+" "+sz+")");
}
}
$(function() {
var elm="a[href$='.pdf'],";
"a[href$='.doc'],"+
"a[href$='.ppt'],"+
"a[href$='.xls'],"+
"a[href$='.docx'],"+
"a[href$='.pptx'],"+
"a[href$='.xlsx']";
$(elm).each(function(i, e) {
if (e.hostname && e.hostname == location.hostname) {
$.ajax({
type: "HEAD",
url: $(this).attr("href"),
complete: function(xhr, textStatus) {
var cl=xhr.getResponseHeader("content-length");
if(textStatus=="success") {
var cl=xhr.getResponseHeader("content-length");
hdrDetails(i, elm, cl);
}else{
$(elm).eq(i).append(" (file not found)");
}
}
});
}
if (e.hostname && e.hostname !== location.hostname) { //external link
//add the icon
// $(this).after();
//call json-head
var url= "http://json-head.appspot.com/?url="+encodeURI($(this).attr('href'))+"&callback=?";
$.getJSON(url, function(json){
if(json.ok && json.headers["Content-Length"] && json.status_code == 200) {
var cl = json.headers["Content-Length"];
hdrDetails(i, elm, cl); //run the calculations and insert a file size
}
if(json.ok && json.status_code == 404) { //file not found error
$(elm).eq(i).append(" (file not found)");
}
});
}
});
});