Skip to content

Instantly share code, notes, and snippets.

@ig10
Last active December 27, 2015 00:59
Show Gist options
  • Save ig10/7241260 to your computer and use it in GitHub Desktop.
Save ig10/7241260 to your computer and use it in GitHub Desktop.
JS+jQuery Cookie Handler
var Cookie = {
existe: function(nombre){
var cookies = document.cookie.split(';');
var existe = false;
$j.each(cookies, function(c, v){
if($j.trim(v).split('=')[0] == nombre){
existe = true;
}
});
return existe;
},
obtener: function(nombre){
var cookies = document.cookie.split(';');
var valor = "";
$j.each(cookies, function(c, v){
if($j.trim(v).split('=')[0] == nombre){
valor = $j.trim(v).split('=')[1];
}
});
return valor;
},
borrar: function(nombre){
var borrada = false;
if (Cookie.existe(nombre)){
document.cookie = nombre + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
borrada = true;
}
return borrada;
},
agregar_temporal: function(nombre,minutos){
var ahora = new Date();
ahora.setMinutes(ahora.getMinutes() + minutos);
document.cookie = nombre + '=1;expires='.concat(ahora.toGMTString());
return true;
},
expirar: function(nombre,tiempo,unidad){
// Unidad: dias, horas, minutos
var expirada = false;
map = {"dias":"Date","horas":"Hours","minutos":"Minutes"};
if(Cookie.existe(nombre)){
var fecha = new Date();
eval("fecha.set"+ map[unidad] +"(fecha.get"+ map[unidad] + "() + " + tiempo.toString() + ")");
document.cookie = nombre + "=;expires=" + fecha.toGMTString();
expirada = true;
}
return expirada;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment