Get cookie by name

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

    Just use the following function (a pure javascript code)

    const getCookie = (name) => {
     const cookies = Object.assign({}, ...document.cookie.split('; ').map(cookie => {
        const name = cookie.split('=')[0];
        const value = cookie.split('=')[1];
    
        return {[name]: value};
      }));
    
      return cookies[name];
    };
    

提交回复
热议问题