JS中cookie的各種操作,JScookie操作

來源:互聯網
上載者:User

JS中cookie的各種操作,JScookie操作
<script>
/* 如果沒有cookie資訊,將顯示div,否則不顯示。 (請在Firefox瀏覽器下測試) */
//設定cookie
function setCookie(name, value, iDay){
    var oDate=new Date();
    oDate.setDate(oDate.getDate()+iDay);
document.cookie=name+'='+value+';expires='+oDate;    
}

//讀取cookie
function getCookie(name)
{
    var arr=document.cookie.split(';');  //定義一個數組
    var re=new RegExp('\\b'+name+'=\\w+');  //定義一個正則
    var res=document.cookie.match(re);  //匹配所選欄位
    if(res){
        return res[0].split('=')[1]; //如果匹配成功,返回結果
    }
    else
    {
        return ''; //否則返回空
    }
}

//刪除cookie
function removeCookie(name){setCookie(name, '1', -1) } //利用setCookie函數,將時間設定為-1,達到刪除效果(預設沒有刪除方法)

window.onload=function ()
{
    var oDiv=document.getElementById('div1');
    
    if(!getCookie('arrial'))
    {
        oDiv.style.display='block';
         setCookie('arrial', '1', 30);      //向cookie中添加'arrial', '1', 30
    }
};
</script>file:///home/hezhenzhen/.cache/.fr-zZlGID/%C3%90%C3%82%C3%8A%C3%96%C2%B5%C2%BC%C2%BA%C2%BD%C2%BF%C3%8E%C2%B3%C3%8C%C3%94%C2%B4%C2%B4%C3%BA%C3%82%C3%AB/index.html

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.