Skip to content

Instantly share code, notes, and snippets.

@rainbowstudio
Created March 18, 2014 21:59
Show Gist options
  • Save rainbowstudio/9630658 to your computer and use it in GitHub Desktop.
Save rainbowstudio/9630658 to your computer and use it in GitHub Desktop.
Affichage du type et de la taille des documents pdf
function hdrDetails(index, elm, length, type) {
// divide the length into its largest unit
var units = [
[1024 * 1024 * 1024, 'Go'],
[1024 * 1024, 'Mo'],
[1024, 'Ko'],
[1, 'bytes']
];
for(var i = 0; i < units.length; i++){
var unitSize = units[i][0];
var unitText = units[i][1];
if (length >= unitSize) {
length = length / unitSize;
// 1 decimal place
length = Math.ceil(length * 10) / 10;
var lengthUnits = unitText;
break;
}
}
var len = $(elm).eq(index).text().length; //check the link’s text length
if(len > 0) {
$(elm).eq(index).wrap('<span class="doc-link '+type+'-link" />');
//add a file size
$(elm).eq(index)
.before('<i class="icon-download"></i> ')
.after(' <span class="size">('+type+', '+length+' '+lengthUnits+')</span>');
}
}
$(function() {
var elm="a[href$='.pdf']"
$(elm).each(function(index, value) {
var bits = this.href.split('.');
var type = bits[bits.length -1];
// On vérifie que le lien vers le fichier ne pointe pas sur un site externe
if (value.hostname && value.hostname == location.hostname) {
$.ajax({
type: "HEAD",
url: $(this).attr("href"),
cache :true,
complete: function(xhr, textStatus) {
var length=xhr.getResponseHeader("content-length");
if(xhr.status == 200) {
hdrDetails(index, elm, length,type); //call the calculation fn
}else{
$(elm).eq(index).after("<span> (Fichier introuvable)</span>");
}
}
});
}
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment