Get cookie by name

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

    Here is a one liner to get a cookie with a specific name without the need of any external lib:

    var cookie = ("; "+document.cookie).split("; YOUR_COOKIE_NAME=").pop().split(";").shift();
    

    This answer is based on kirlich's brilliant solution. The only compromise of this solution is, that you will get an empty string when the cookie does not exist. In most cases this should not be a deal breaker, though.

提交回复
热议问题