Get cookie by name

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

    kirlich gave a good solution. However, it fails when there are two cookie values with similar names, here is a simple fix for this situation:

    function getCookie(name) {
      var value = "; " + document.cookie;
      var parts = value.split("; " + name + "=");
      if (parts.length >= 2) return parts.pop().split(";").shift();
    }
    

提交回复
热议问题