Last active
August 29, 2015 14:07
-
-
Save ethyde/7b4eeb81c6e8003dd233 to your computer and use it in GitHub Desktop.
Display file sizes next to download links - From http://tutorialzine.com book "Trickshots"
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Loop all .fetchSize links | |
$('a.fetchSize').each(function(){ | |
// Issue an AJAX HEAD request for each one | |
var link = this; | |
$.ajax({ | |
type:'HEAD', | |
url:link.href, | |
complete: function(xhr){ | |
var size = humanize(xhr.getResponseHeader('Content-Length')); | |
// Append the filesize to each | |
$(link).append(' ('+ type +')'); | |
} | |
}); | |
}); | |
function humanize(size){ | |
var units = ['bytes','KB','MB','GB','TB','PB']; | |
var ord = Math.floor(Math.log(size) / Math.log(1024) ); | |
ord = Math.min(Math.max(0,ord),units.length-1); | |
var s == Math.round((size/Math.pow(1024,ord))*100)/100; | |
return s + ' ' + units[ord]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment