Questo è interessante perchè molto semplice (trovato qui tempo fa
)
codice:
/** http://www.devpro.it/code/106.html **
* Object Cookie
* fast and simple way to manage one or more cookie from javascript.
*
* NOTE:
* This object escape values then saved var will be always a string.
* To storage complex variables or to preserve saved variable informations
* you should combine this object with PHP_Serializer.
* [ http://www.devpro.it/javascript_id_102.html ]
* _____________________________________________
*
* QUICK EXAMPLE:
* Cookie.set('var', 'value');
* alert(Cookie.get('var'));
* Cookie.unset('var');
*
* EXAMPLE WITH PHP_Serializer OBJECT:
* var php = new PHP_Serializer();
* var complexVar = [true, 'test', 12345];
* Cookie.set('complexVar', php.serialize(complexVar));
* alert( typeof php.unserialize( Cookie.get('complexVar') ) );
* // will be object and not string then returned value is an array
* Cookie.unset('complexVar');
* ---------------------------------------------
* @author Andrea Giammarchi
* @site www.devpro.it
* @date 2005/12/19 11:00
* @lastmod 2005/12/19 11:00
* @credits Special thanks to Dustin Diaz
* [ http://www.dustindiaz.com/top-ten-javascript/ ]
* @version 1.0 tested FireFox 1.5, IE 6 SP2 and Opera 8
*/
var Cookie = {
/**
* public method, returns value for chosed name or false if is not present
* Cookie.get( cookiename:String ):String
* @param String name of saved cookie
* @return String value saved with chosed name
*/
get: function(name) {
var dc = document.cookie;
var cl = dc.indexOf(name+'=')+name.length;
if(++cl > name.length) {
var ce = dc.indexOf(';',cl);
if(ce == -1)
ce = dc.length;
cl = unescape(dc.substr(cl,(ce-cl)));
}
else
cl = false;
return cl;
},
/**
* public method, sets a cookie
* Cookie.set( name:String, value:Mixed[, exp:Int[, path:String[, domain:String[, secure:Boolean ]]]]):Void
* @param String name of cookie
* @param Mixed cookie value
* @param Int expiration date ( 0, 1, ... N days )
* @param String valid path for this cookie
* @param String domain for this cookie
* @param Boolean secure ( for example in https sites )
*/
set: function(name, value, exp, path, domain, secure) {
var c = new Array();
c.push(name + '=' + escape(value));
var e = (exp > 0);
if(e) {
var n = new Date();
exp = new Date(n.getTime()+(exp*86400000));
c.push('expires='+exp.toGMTString());
}
if(path){c.push('path='+path);}
if(domain){c.push('domain='+domain);}
if(secure){c.push('secure');}
document.cookie = c.join(';');
},
/**
* public method, removes a cookie if present.
* Cookie.unset( name:String[, path:String[, domain:String]] ):Boolean
* @param String saved cookie name
* @param String path used for this cookie
* @param String domain used for this cookie
* @return Boolean true if was present and is removed or false
*/
unset: function(name, path, domain) {
var result = this.get(name);
if(result != false) {
var c = new Array();
c.push(name + '=');
if(path){c.push('path=' + path);}
if(domain){c.push('domain=' + domain);}
c.push('expires=Thu, 01-Jan-1970 00:00:01 GMT');
document.cookie = c.join(';');
}
return !!result;
}
}
:quote: c'è pure l'esempio
* QUICK EXAMPLE:
* Cookie.set('var', 'value');
* alert(Cookie.get('var'));
* Cookie.unset('var');