Get cookie by name

前端 未结 30 1986
野的像风
野的像风 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:15

    use a cookie getting script:

    function readCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
    

    then call it:

    var value = readCookie('obligations');
    

    i stole the code above from quirksmode cookies page. you should read it.

提交回复
热议问题