Get cookie by name

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

    My solution is this:

    function getCookieValue(cookieName) {
        var ca = document.cookie.split('; ');
        return _.find(ca, function (cookie) {
            return cookie.indexOf(cookieName) === 0;
        });
    }
    

    This function uses the Underscorejs _.find-function. Returns undefined if cookie name doesn't exist

提交回复
热议问题