Accueil > > > UNE CLASS COOKIE JUSTE POUR RIRE
UNE CLASS COOKIE JUSTE POUR RIRE
Information sur la source
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
Sources de la même categorie
Commentaires et avis
|
Derniers Blogs
[MIX10] KEYNOTE DEUXIèME JOURNéE - INTERNET EXPLORER 9, HTML5, VISUAL STUDIO 2010, ODATA[MIX10] KEYNOTE DEUXIèME JOURNéE - INTERNET EXPLORER 9, HTML5, VISUAL STUDIO 2010, ODATA par cyril
Le deuxième keynote du mix fut très riche en contenu. Internet Explorer 9 Juste un après le lancement de Internet Explorer 8, Microsoft a dévoilé les nouveautés de Internet Explorer 9. Désormais, IE supportera HTML5, SVG et CSS3. L'élément ...
Cliquez pour lire la suite de l'article par cyril CERTIFICATIONS BETA .NET 4CERTIFICATIONS BETA .NET 4 par KooKiz
Les inscriptions pour les certifications beta .NET 4 ont commencé. L'inscription est offerte pour les examens suivants : - 71-511, TS: Windows Applications Development with Microsoft .NET Framework 4 - 71-515, TS: Web Applications Development with...
Cliquez pour lire la suite de l'article par KooKiz [MIX 2010] - MICROSOFT TRANSLATOR TECHNOLOGY PREVIEW V2[MIX 2010] - MICROSOFT TRANSLATOR TECHNOLOGY PREVIEW V2 par redo
J'imagine que la plupart d'entre vous connaissent bien et utilisent le service de traduction de Google, mais connaissez-vous celui de Microsoft . Microsoft Translator ? Effectivement, Microsoft nous annoncé le lancement version 2 de la Technologie Preview...
Cliquez pour lire la suite de l'article par redo LANCEMENT EN PREVIEW DE CYCLONE LORS DES TECHDAYS 2010!LANCEMENT EN PREVIEW DE CYCLONE LORS DES TECHDAYS 2010! par MPOWARE
Toutes les vidéos de ce lancement sont en ligne!
Partie I - Intro
http://www.youtube.com/watch?v=LkQzTQ8T6CA
Partie II - Démo 1
http://www.youtube.com/watch?v=drAhYQ7lqvo
Partie III - Démo 2
http://www.youtube.com/watch?v=c8KM_1Gqybc...
Cliquez pour lire la suite de l'article par MPOWARE
Logiciels
Academy System (10.9.4.0)ACADEMY SYSTEM (10.9.4.0)Logiciel de gestion des établissements.
- élèves/étudiants (inscription, dossier, absence...)
-... Cliquez pour télécharger Academy System Xilisoft Convertisseur Vidéo Ultimate (5.1.39.0305)XILISOFT CONVERTISSEUR VIDéO ULTIMATE (5.1.39.0305)Xilisoft Convertisseur Vidéo Ultimate est un outil puissant de conversion vidéo, facile à utilise... Cliquez pour télécharger Xilisoft Convertisseur Vidéo Ultimate Xilisoft DVD Ripper Ultimate (5.0.64.0304)XILISOFT DVD RIPPER ULTIMATE (5.0.64.0304)Xilisoft DVD Ripper Ultimate est un logiciel excellent pour copier et convertir DVD vers presque ... Cliquez pour télécharger Xilisoft DVD Ripper Ultimate Rigs of Rods (63.3)RIGS OF RODS (63.3)c'est un jeu de multi-simulation camions,autobus voitures, avions, bateaux, hélicoptère avec défo... Cliquez pour télécharger Rigs of Rods
|