Bubble Sweat Bubble

Les périgrinations d'un mec normal dans un monde qui ne l'est franchement pas

Aller au contenu | Aller au menu | Aller à la recherche

Mot clé - javascript

Fil des billets - Fil des commentaires

07juin

sprintf en Javascript

Javascript ne propose pas en standard de fonction sprintf pourtant si pratique.... Voilà le code de cette fonction trouvée sur http://jan.moesen.nu/ :
// This code is in the public domain. Feel free to link back to http://jan.moesen.nu/
function sprintf() {
if (!arguments || arguments.length < 1 || !RegExp) {
return;
}

var str = arguments[0];
var re = /([^%]*)%('.|0|\x20)?(-)?(\d+)?(\.\d+)?(%|b|c|d|u|f|o|s|x|X)(.*)/;
var a = b = [], numSubstitutions = 0, numMatches = 0;
while (a = re.exec(str)) {
var leftpart = a[1], pPad = a[2], pJustify = a[3], pMinLength = a[4];
var pPrecision = a[5], pType = a[6], rightPart = a[7];

numMatches++;

if (pType == '%') {
subst = '%';
} else {
numSubstitutions++;
if (numSubstitutions >= arguments.length) {
alert('Error! Not enough function arguments (' + (arguments.length - 1) + ', excluding the string)\nfor the number of substitution parameters in string (' + numSubstitutions + ' so far).');
}

var param = arguments[numSubstitutions];
var pad = '';
if (pPad && pPad.substr(0,1) == "'") {
pad = leftpart.substr(1,1);
} else if (pPad) {
pad = pPad;
}

var justifyRight = true;
if (pJustify && pJustify === "-") {
justifyRight = false;
}

var minLength = -1;
if (pMinLength) {
minLength = parseInt(pMinLength);
}

var precision = -1;
if (pPrecision && pType == 'f') {
precision = parseInt(pPrecision.substring(1));
}

var subst = param;
if (pType == 'b') {
subst = parseInt(param).toString(2);
} else if (pType == 'c') {
subst = String.fromCharCode(parseInt(param));
} else if (pType == 'd') {
subst = parseInt(param) ? parseInt(param) : 0;
} else if (pType == 'u') {
subst = Math.abs(param);
} else if (pType == 'f') {
subst = (precision > -1) ? Math.round(parseFloat(param) * Math.pow(10, precision)) / Math.pow(10, precision): parseFloat(param);
} else if (pType == 'o') {
subst = parseInt(param).toString(8);
} else if (pType == 's') {
subst = param;
} else if (pType == 'x') {
subst = ('' + parseInt(param).toString(16)).toLowerCase();
} else if (pType == 'X') {
subst = ('' + parseInt(param).toString(16)).toUpperCase();
}
}

str = leftpart + subst + rightPart;
}

return str;
}

16aoû

Lightbox ou comment attirer l'attention

Pour ceux qui se demande comment réaliser un effet de boîte de dialogue rendant inaccessible ce qui est derrière lui, autrement dit un comportement basique de n'importe quel système de fenêtres, une solution en javascript se nomme Lightbox.

Il s'agit d'une bibliothèque javascript qui permet d'afficher une boîte de dialogue de façon élégante et rapide de façon très simple. On peut l'utiliser en lui donnant une url ou charger ses données ou alors les lui donner directement.
Pour ceux qui ont déjà fait de jolis formulaires on peut aussi très facilement les relier au système lightbox.
Seul bémol en ce qui me concerne : le code n'est pas écrit en objet.