Skip to content

Instantly share code, notes, and snippets.

@uolcano
Created August 5, 2016 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save uolcano/2bb80cbe48e93cea31920184d38008cf to your computer and use it in GitHub Desktop.
Save uolcano/2bb80cbe48e93cea31920184d38008cf to your computer and use it in GitHub Desktop.
Some cookie method set
var uTools = {};
/*****************
The cookie method set
*****************/
! function ($) {
$.cookies = {
get: function (name) {
var cookies = document.cookie,
name = encodeURIComponent(name) + '=',
start = cookies.indexOf(name),
end = null,
value = null;
if (start > -1) {
end = cookies.indexOf('; ', start);
end === -1 && (end = cookies.length);
value = decodeURIComponent(cookies.slice(start, end));
}
return value;
},
set: function (name, value, maxAge, path, domain, secure) {
var cookie = '';
if (typeof name == 'undefined' || typeof value == 'undefined')
throw Error('The name and value must be defined.');
cookie += encodeURIComponent(String(name)) +
'=' + encodeURIComponent(String(value));
typeof maxAge == 'number' &&
cookie += '; max-age=' + maxAge;
typeof path == 'string' && cookie += '; path=' + path;
typeof domain == 'string' && cookie += '; domain=' + domain;
typeof secure == 'boolean' && cookie += '; secure';
document.cookie = cookie;
},
unset: function (name, path, domain, secure) {
this.set(name, '', +new Date(), path, domain, secure);
}
};
}(uTools);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment