Skip to content

Instantly share code, notes, and snippets.

@betawax
Created July 4, 2012 15:14
Show Gist options
  • Save betawax/3047846 to your computer and use it in GitHub Desktop.
Save betawax/3047846 to your computer and use it in GitHub Desktop.
Create and retrieve cookies with JavaScript
function setCookie(name, value, path, expires) {
var date = new Date;
date.setTime(date.getTime()+(expires*24*60*60*1000));
document.cookie = name+"="+value+";path="+path+";expires="+date.toGMTString();
}
function getCookie(name) {
if (document.cookie) {
var value = document.cookie.split(name+"=")[1];
if (value) {
return value.split(";")[0];
}
}
return false;
}
@betawax
Copy link
Author

betawax commented Jul 4, 2012

Define the param "expires" in days.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment