How do I create and read a value from cookie?

前端 未结 19 1677
梦如初夏
梦如初夏 2020-11-21 04:42

How can I create and read a value from a cookie in JavaScript?

19条回答
  •  时光说笑
    2020-11-21 05:12

    A cheeky and simple way of reading a cookie could be something like:

    let username, id; 
    eval(document.cookie); 
    console.log(username + ", " + id); // John Doe, 123
    

    This could be used if you know your cookie contains something like: username="John Doe"; id=123;. Note that a string would need quotes in the cookie. Not the recommended way probably, but works for testing/learning.

提交回复
热议问题