天天看點

cookie getCookie setCookie deleteCookie

cookie getCookie setCookie deleteCookie

每個cookie檔案的名字都是其網站的域名

cookie getCookie setCookie deleteCookie
cookie getCookie setCookie deleteCookie
<script>
    document.cookie="cookieName = cookieValue;";
    document.cookie=" cookieName2 = cookieValue2";
    document.cookie=" a2 = "+encodeURIComponent("cookieValue3;");   //就類似加密   解密decodeURIComponent
    document.cookie=" a3 = "+encodeURI("cookieValue4");
    //cookieName = cookieValue;   a2 = cookieValue3;;   a3 = cookieValue4;
    alert(document.cookie);

    //取cookie特定cookie的值
    function getCookie(cookieName) {                                      //這裡其實就是我們之前使用函數的源碼,若要真正去使用cookie
        var start = document.cookie.indexOf(cookieName+"=");           //可以去看cookie的文檔 其實html5已經把這些函數封裝好了  直接可以調用
        if(start == -1){return "";}
        start = start+cookieName.length+1;
        var end = document.cookie.indexOf(";",start);
        if(-1==end){end = document.cookie.length;}
        return decodeURIComponent(document.cookie.substring(start,end));
    }
    alert(getCookie('a2'));

    //一個完整的cookie
    var expires = new Date();
    expires.setMonth(expires.getMonth()+1);  //一個月後Cookie失效
    document.cookie = "username="+encodeURI("使用者名")+";expires ="+
                    expires.toGMTString()+";path = /;domain=x-life.com;secure";
</script>