Skip to content

Instantly share code, notes, and snippets.

@d2m
Created February 28, 2012 21:42
Show Gist options
  • Save d2m/1935339 to your computer and use it in GitHub Desktop.
Save d2m/1935339 to your computer and use it in GitHub Desktop.
dart document.cookie lib
/*
* dart document.cookie lib
*
* ported from
* http://www.quirksmode.org/js/cookies.html
*
*/
void createCookie(String name, String value, int days) {
String expires;
if (days != null) {
Date now = new Date.now();
Date date = new Date.fromEpoch(now.value + days*24*60*60*1000, new TimeZone.local());
expires = '; expires=' + date.toString();
} else {
Date then = new Date.fromEpoch(0, new TimeZone.utc());
expires = '; expires=' + then.toString();
}
document.cookie = name + '=' + value + expires + '; path=/';
}
String readCookie(String name) {
String nameEQ = name + '=';
List<String> ca = document.cookie.split(';');
for (int i = 0; i < ca.length; i++) {
String c = ca[i];
c = c.trim();
if (c.indexOf(nameEQ) == 0) {
return c.substring(nameEQ.length);
}
}
return null;
}
void eraseCookie(String name) {
createCookie(name, '', null);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment