首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何删除Dart中的cookie

如何删除Dart中的cookie
EN

Stack Overflow用户
提问于 2013-08-28 16:40:43
回答 1查看 1.5K关注 0票数 2

在客户端上运行时,如何删除dart中的cookie?

我尝试删除它,方法是将它设置为空字符串。

代码语言:javascript
复制
document.cookie = 'cookie_to_be_deleted=""';

如果在这一行之后打印cookie的值,就会得到带有两个"cookie_to_be_deleted“实例的键值对的分号分隔列表。其中一个具有我希望删除的原始值,另一个值为空字符串。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-08-28 19:47:44

试试这个:

代码语言:javascript
复制
Date then = new Date.fromEpoch(0, new TimeZone.utc());
document.cookie = 'cookie_to_be_deleted=; expires=' + then.toString() + '; path=/';

https://gist.github.com/d2m/1935339找到了这些实用程序

代码语言:javascript
复制
/* 
 * 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);
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/18493639

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档