-
-
Save nikgraf/5437970 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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