Read a javascript cookie by name

后端 未结 11 1004
盖世英雄少女心
盖世英雄少女心 2020-12-05 06:50

I have set a cookie using

document.cookie = 
    \'MYBIGCOOKIE=\' + value + 
    \'; expires=\' + now.toGMTString() + 
    \'; path=/\';

No

11条回答
  •  渐次进展
    2020-12-05 07:23

    Unfortunately, Javascript's cookie syntax is nowhere near as nice as that. In fact, in my opinion, it's one of the worst designed parts.

    When you try to read document.cookie, you get a string containing all the cookies set. You have to parse the string, separating by the semicolon ; character. Rather than writing this yourself, there are plenty of versions available on the web. My favourite is the one at quirksmode.org. This gives you createCookie, readCookie and deleteCookie functions.

提交回复
热议问题