Skip to content

Instantly share code, notes, and snippets.

@malko
Created May 4, 2012 14:15
Show Gist options
  • Save malko/2595040 to your computer and use it in GitHub Desktop.
Save malko/2595040 to your computer and use it in GitHub Desktop.
javascript cookies
cookies={
get:function(name){
var re=new RegExp(name+'=([^;]*);?','gi'), result=re.exec(document.cookie)||[];
return (result.length>1? unescape(result[1]): false);
},
set:function(name, value, expirationTime, path, domain, secure){
var time=new Date();
if(expirationTime){
time.setTime(time.getTime()+(expirationTime*1000));
}
document.cookie=name+ '='+ escape(value)+ '; '+
(!expirationTime? '': '; expires='+time.toUTCString())+
'; path='+(path?path:'/')+ (!domain? '': '; domain='+domain)+ (!secure? '': '; secure');
},
del:function(name, path, domain){
var value=this.get(name);
document.cookie=name+ '='+ '; path='+(path?path:'/')+
(!domain? '': '; domain='+domain)+
'; expires=Thu, 01-Jan-70 00:00:01 GMT';
return value;
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment