Skip to content

Instantly share code, notes, and snippets.

@invmatt
Created May 5, 2016 09:55
Show Gist options
  • Save invmatt/ab5c8a9be43bcc797befb77bd4d89e26 to your computer and use it in GitHub Desktop.
Save invmatt/ab5c8a9be43bcc797befb77bd4d89e26 to your computer and use it in GitHub Desktop.
Quick way to set/get cookies in JS
/**
* Set/Get cookie functions
* setCookie('test','1');
* getCookie('test');
*/
function setCookie(key, value) {
var expires = new Date();
expires.setTime(expires.getTime() + (1 * 24 * 60 * 60 * 1000));
document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
}
function getCookie(key) {
var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
return keyValue ? keyValue[2] : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment