Read cookies with JavaScript

前端 未结 3 1500
既然无缘
既然无缘 2020-12-06 14:42

I know how to write/create cookies in JavaScript.........................................................

//Create the cookies
document.cookie = \"Name=\" +          


        
3条回答
  •  旧时难觅i
    2020-12-06 15:43

    From http://w3schools.com/js/js_cookies.asp

    set cookie

    function setCookie(c_name,value,expiredays)
    {
    var exdate=new Date();
    exdate.setDate(exdate.getDate()+expiredays);
    document.cookie=c_name+ "=" +escape(value)+
    ((expiredays==null) ? "" : ";expires="+exdate.toUTCString());
    }
    

    get cookie

    function getCookie(c_name)
    {
    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 "";
    }
    

提交回复
热议问题