Skip to content

Instantly share code, notes, and snippets.

@nikgraf
Forked from d2m/gist:1935339
Created April 22, 2013 19:56
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 nikgraf/5437970 to your computer and use it in GitHub Desktop.
Save nikgraf/5437970 to your computer and use it in GitHub Desktop.
library cookie;
import 'dart:html';
/*
* 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) {
DateTime now = new DateTime.now();
DateTime date = new DateTime.fromMillisecondsSinceEpoch(now.millisecondsSinceEpoch + days*24*60*60*1000);
expires = '; expires=' + date.toString();
} else {
DateTime then = new DateTime.fromMillisecondsSinceEpoch(0, isUtc: true);
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