2013-08-28 4 views
2

클라이언트에서 실행할 때 다트의 쿠키를 삭제하려면 어떻게해야합니까?다트에서 쿠키를 삭제하는 방법

나는 내가이 줄 끝에서 쿠키의 값을 인쇄 할 경우 내가 두 인스턴스 키 값 쌍의 세미콜론으로 구분 된 목록을

document.cookie = 'cookie_to_be_deleted=""'; 

를 사용하여 빈 문자열로 설정하여 삭제 시도 "cookie_to_be_deleted". 하나는 내가 삭제하기를 원했던 원래 값을 가지고 있고 다른 하나는 그 값을위한 빈 문자열을 가지고 있습니다.

답변

6

이 시도 :

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); 
} 
+0

에서 이러한 유틸리티를 발견했다. 그러나 약간의 수정을해야했다. readCookie를 한번도 시도한 적이 없지만 createCookie에서 Date 객체로 Date 객체를 변경해야했으며 fromEpoch 함수 대신'now.add (days : days); '를 사용했습니다. – Spaceindaver

관련 문제