Skip to content

Instantly share code, notes, and snippets.

@colelawrence
Forked from jrivero/jsCookies.js
Last active August 29, 2015 14:07
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 colelawrence/03bc26233e68f4e5291f to your computer and use it in GitHub Desktop.
Save colelawrence/03bc26233e68f4e5291f to your computer and use it in GitHub Desktop.
// found on http://snipplr.com/view/36790/jscookies--my-simple-easy-pure-js-javascript-cookies-function/
// Modified with Regexes by Cole Lawrence (ZombieHippie)
// create my jsCookies function
var jsCookies = {
// this gets a cookie and returns the cookies value, if no cookies it returns blank ""
get: function(c_name) {
var regex = new RegExp(c_name + "=([^;]+)");
var match = regex.exec(document.cookie);
return match != null ? unescape(match[1]) : null;
},
// this sets a cookie with your given ("cookie name", "cookie value", "good for x days")
set: function(c_name, value, expiredays) {
expiredays = expiredays || 365;
var exdate = new Date();
exdate.setDate(exdate.getDate() + expiredays);
document.cookie = c_name + "=" + escape(value) + ((expiredays == null) ? "" : "; expires=" + exdate.toUTCString());
},
// this checks to see if a cookie exists, then returns true or false
check: function(c_name) {
return !!jsCookies.get(c_name)
}
};
// end my jsCookies function
// USAGE - get :: jsCookies.get("cookie_name_here"); [returns the value of the cookie]
// USAGE - set :: jsCookies.set("cookie_name", "cookie_value", 5 ); [give name, val and # of days til expiration]
// USAGE - check :: jsCookies.check("cookie_name_here"); [returns only true or false if the cookie exists or not]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment