begin process at 2010 03 18 21:14:26
  Trouver un code source :
 
dans
 
Accueil > 

Code

 > 

Cookies

 > UNE CLASS COOKIE JUSTE POUR RIRE

UNE CLASS COOKIE JUSTE POUR RIRE


 Information sur la source

Note :
9,5 / 10 - par 2 personnes
9,50 / 10

  • 1

  • 2

  • 3

  • 4

  • 5

  • 6

  • 7

  • 8

  • 9

  • 10
Catégorie :Cookies Niveau :Initié Date de création :19/10/2003 Vu :7 655

Auteur : reblochon

Ecrire un message privé
Site perso
Commentaire sur cette source (4)
Ajouter un commentaire et/ou une note

 Description

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

  • 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;
  • }
  • }
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


 Sources du même auteur

GESTION DES DATES
INFORMATION SUR LES INPUTS DE FORMULAIRE
Source avec Zip CLACULATEUR DE CHMOD

 Sources de la même categorie

Source avec Zip LES COOKIES ET JAVASCRIPT par Zestyr
COOKIES EN JAVASCRIPT par CodeurleGeek
Source avec Zip MULTI-BLOC-NOTE(AVEC COOKIES!) par DomJ
Source avec Zip LES VINGTS JOLIS COOKIES par cornofulgur
Source avec Zip AJOUTER, SUPPRIMER, LIRE COOKIE! par DomJ

Commentaires et avis

Commentaire de reblochon le 19/10/2003 23:24:15

J'oubliais.... Au cas ou des molusques auaient la bonne idee de donné comme format un integer, au nom de leur cookie, remplacez la ligne:
            var res = str.match(/./g);
par la ligne:
            var res = str.toString().match(/./g);
dans la fonction addBckSlash

merci

On n'est jamais à l'abris d'un Georges W Bush.

Philippe

Commentaire de roro06 le 28/04/2004 16:13:34

Je met une bonne note car j'ai pas mal appris grace a ce source, non pas sur les cookies, mais sur les "classes"

Commentaire de reblochon le 28/04/2004 17:32:02

C'etait le but aussi.

En realité, les cookies sont bien plus simple d'utilisation. Des dizaines de codes (souvent identiques) se trouvent facilement sur le net pour les gerer. L'idee de prendre des class etait plutot didactitielle.

Un autre exemple simple, mais interessant car il recapitule les grandes lignes du fonctionnement des class, se trouve aussi ici: http://pages.infinit.net/glopglop/local/exemple/objects.html.
C'est celui dont je me sers quand j'ai un trou de memoire (ou plutot un overflow, vu le nombre de langages que je pratique en meme temps)

C'est du code js à coller dans une page html, ou en utilisant un editeur. Le mien Online, pour faire des tests, js à gauche, html à droite, aller ici: http://pages.infinit.net/glopglop/local/test_ver03.html
Pas de critique, c'est un truc maison, ancien, de base et dont je ne decrirais pas tous les petits trucs cachés. En resumé, c'est à moi :O).

Merci pour la note, je prends ca comme un encouragement à aider les autres.

Commentaire de hakim0 le 10/12/2006 17:56:51

Bonn code,
je ve remplace ":)" sur une chaine avec "rire"
Var str=":) test!";
Var reg = new RegExp("\:\)","g");
str=str.replace(reg,"rire");
//////////
mais ca marche pas! j'ai besoin d'aide???
Merci.

 Ajouter un commentaire




Nos sponsors


Sondage...

Comparez les prix

CalendriCode

Mars 2010
LMMJVSD
1234567
891011121314
15161718192021
22232425262728
293031    

Consulter la suite du CalendriCode

Photothèque

 
Développement réalisé par Nicolas SOREL (Nix) avec l'aide de : Cyril DURAND et Emmanuel (EBArtSoft), Merci à Vincent pour ses précieux conseils.
CodeS-SourceS.com© Toute reproduction même partielle est interdite sauf accord écrit du Webmaster
CodeS-SourceS.com© est une marque déposée tous droits réservés

Google Coop CodeS-SourceS Google Coop CodeS-SourceS
Temps d'éxécution de la page : 0,437 sec (3)

Nous contacter | Annoncer sur CodeS-SourceS | Mentions légales