Get cookie by name

前端 未结 30 1760
野的像风
野的像风 2020-11-22 03:58

I have a getter to get the value from a cookie.

Now I have 2 cookies by the name shares= and by the name obligations= .

I want to

30条回答
  •  野性不改
    2020-11-22 04:27

    There are already nice answers here for getting the cookie,However here is my own solution :

    function getcookie(cookiename){
    var cookiestring  = document.cookie;
    var cookiearray = cookiestring.split(';');
    for(var i =0 ; i < cookiearray.length ; ++i){ 
        if(cookiearray[i].trim().match('^'+cookiename+'=')){ 
            return cookiearray[i].replace(`${cookiename}=`,'').trim();
        }
    } return null;
    }
    

    usage :`

         getcookie('session_id');
       // gets cookie with name session_id
    

提交回复
热议问题