2
如何在客戶端上運行時刪除飛鏢中的cookie?如何刪除飛鏢中的cookie
我試圖將其設置爲使用
document.cookie = 'cookie_to_be_deleted=""';
一個空字符串來刪除它。如果我這一行後打印cookie的值,我得到的鍵值對的分號分隔的列表用兩個實例「cookie_to_be_deleted」。其中一個具有我希望刪除的原始值,另一個具有值爲空的字符串。
如何在客戶端上運行時刪除飛鏢中的cookie?如何刪除飛鏢中的cookie
我試圖將其設置爲使用
document.cookie = 'cookie_to_be_deleted=""';
一個空字符串來刪除它。如果我這一行後打印cookie的值,我得到的鍵值對的分號分隔的列表用兩個實例「cookie_to_be_deleted」。其中一個具有我希望刪除的原始值,另一個具有值爲空的字符串。
試試這個:
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
/*
* 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);
}
這爲我找到這些實用程序。不得不做一些修改。我從來沒有嘗試過readCookie,但在createCookie中,我必須將Date對象更改爲DateTime對象,而不是使用'now.add(days:days);'的fromEpoch函數。 – Spaceindaver