Une class cookie juste pour rire

Contenu du snippet

Une class js pour les cookie, longue mais simple si on prend le temps de lire.

Ca donne une idee des class, des cookies, des expressions regulieres...etc
Et bien sur du JavaScript.

Bon je m'ennuyais un peu de la prog OO, alors comme j'etais en plein JavaScrip pour un site, j'ai deliré.
Ca sert à rien mais c'est pour la forme. C'est sur qu'en fonctions separées, c'est plus lisible.

Source / Exemple :


function cookieCLASS() { 
// set class before used this  ex: var ck = new cookieCLASS; 
// all arguments with [] are optionals. 
    this.nam; 
    this.val; 
 
    this.get = function(n,u) { 
/* 
ex: ck.get ([string:MY_COOKIE_NAME" | boolean], [boolean]) 
Unescaped the value with arguments [u] = true ex: ck.get("toto", true); 
If you send not argument or if MY_COOKIE_NAME==false, "ck.get" fill the public Arrays "nam" and "val" 

  • /
if (n) { var rN = new RegExp("(?:^|\\;\\s?)(" + addBckSlash(n) + ")\\=?([^\\;]*)"); var res = document.cookie.match(rN); return (RegExp.$1)?((u)?unescape(RegExp.$2):RegExp.$2):false; } else { this.nam = new Array(); this.val = new Array(); var id = 0; var str = document.cookie; while(str != "") { res = /^([^\=\;]+)/.exec(str); if(RegExp.$1) { this.nam[id] = RegExp.$1; this.val[id++] = this.get(RegExp.$1,u); str = (str.search(/\;/) != -1)? str.substring(str.search(/\;/)+1):""; } } return true; } } this.set = function(n) { /* ex: ck.set (string:"MY_COOKIE_NAME", [string:"MY_COOKIE_VALUE"], [string:"/"], [string:".yourdomain.com"], [boolean], [string:"031015" | boolean], [boolean], [boolean]); [0]Name, [1]Value, [2]Path, [3]Domain, [4]secure, [5]Date, [6]escapedValue, [7]escapedName Expire Date Format: arguments[5] is a STRING = "YYMMDD" ck.set(name, value,0,0,0,"031015"); if Date = true "the cookie never expire" else "the cookie expire with the session" Escaped the value with arguments[6] = true ex: ck.set(name, value, 0, 0, 0, 0, true); Escaped the name with arguments[7] = true ex: ck.set(name, value, 0, 0, 0, 0, 0, true);
  • /
var arg = this.set.arguments; var v = p = d = s = e = ""; if(/\w+/.test(n)) { if (arg[1]) v=(arg[6])?escape(arg[1]):arg[1]; if (arg[2]) p=";path=" + arg[2]; if (/(\.\w+)+/.test(arg[3])) d=";domain=" + arg[3]; if (arg[4]) s="; secure"; e = (/(\d{2})(\d{2})(\d{2})/.test(arg[5]))?new Date("20" + RegExp.$1, RegExp.$2-1, RegExp.$3):((arg[5])? new Date(2100,0,1):""); if (e) e="; expires=" + e.toGMTString(); document.cookie = n + "=" +v+p+d+s+e; return (document.cookie.indexOf(n)!=-1)?true:false; } return false; } this.del = function() { /* ex: ck.del(["toto"]); or ck.del(["toto_1", "toto_2", "toto_3"]); Send the cookie's name in argument for delete this. For more cookie, send more name in separate string If you send not argument, all cookie will be deleted.
  • /
arg = this.del.arguments; if (arg.length == 0) { this.get(); arg = this.nam; } var ex=new Date(1999,12,31,23,59,59); //Last second in last millennium, just before the 2ky bug ;0) for (y=0; y < arg.length; y++) { document.cookie=arg[y] + "=;expires=" + ex.toGMTString() + ""; document.cookie=arg[y] + "=;expires=" + ex.toGMTString() + ";path=/"; } } this.nbr = function() { /* ex: ck.nbr(); Return the number of cookie.
  • /
var str = document.cookie; var nbr = 0; while(str != "") { res = /^([^\=\;]+)/.exec(str); if(RegExp.$1) { nbr++; str = (str.search(/\;/) != -1)? str.substring(str.search(/\;/)+1):""; } } return nbr; } function addBckSlash (str) { // PRIVATE use only in this class /* ex: addBckSlash("toto$le?Heraut"); return the string with backslash on all character no alphanumeric([^ a-z A-Z 0-9 _])
  • /
if(str) { var res = str.match(/./g); str = ""; for(y = 0 ; y < res.length ; y++) str += (/\w/.test(res[y]))? res[y]: "\\" + res[y]; return str; } return false; } }

Conclusion :


pour le voir fonctionner...

http://pages.infinit.net/glopglop/local/exemple/cookie.html

bonne journee

coder juste comme ca, pour rien et gratuitement, c'est cool et ca peut aider

Philippe

A voir également

Vous n'êtes pas encore membre ?

inscrivez-vous, c'est gratuit et ça prend moins d'une minute !

Les membres obtiennent plus de réponses que les utilisateurs anonymes.

Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.

Le fait d'être membre vous permet d'avoir des options supplémentaires.