JS中對Cookie的操作詳解

來源:互聯網
上載者:User

標籤:

前言cookie設定

參數說明:
name cookie 名稱,key值
value 可選,cookie 值
expire 可選,到期時間,時間戳記格式
path 可選,伺服器端有效路徑,/ 表示整個網域名稱有效,預設為當前設定 cookie 時頁面的路徑
domain 可選,該 cookie 有效網域名稱
secure 可選。規定是否通過安全的 HTTPS 串連來傳輸 cookie。

代碼的封裝
(function(){    var cookieObj={            ‘add‘:function(name, value, hours){ //修改或是添加cookie                var expire = "";                if(hours != null){ expire = new Date((new Date()).getTime() + hours * 3600000); expire = "; expires=" + expire.toGMTString(); }                               document.cookie = name + "=" + escape(value) + expire + ";path=/";                //如果指定網域名稱可以使用如下                //document.cookie = name + "=" + escape(value) + expire + ";path=/;domain=findme.wang";            },            ‘get‘:function(c_name){//讀取cookie                if (document.cookie.length>0){ c_start=document.cookie.indexOf(c_name + "=") if (c_start!=-1){ c_start=c_start + c_name.length+1 c_end=document.cookie.indexOf(";",c_start) if (c_end==-1){ c_end=document.cookie.length } return unescape(document.cookie.substring(c_start,c_end)) } }                    return "";            }    };    window.cookieObj=cookieObj;}());
調用測試
cookieObj.add(‘myWeb‘,‘http://www.findme.wang‘);console.log(‘myWeb:‘+cookieObj.get(‘website‘));

JS中對Cookie的操作詳解

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.