Any simplest way to get cookie value in javascript

后端 未结 3 1812
Happy的楠姐
Happy的楠姐 2020-12-21 14:22

I search Google but found entire functions to get cookie value. Isn\'t there one line statement to retrieve cookie value in javascript?

相关标签:
3条回答
  • 2020-12-21 14:48

    You can use the following one liner to convert your cookie to a map and then access whatever parameter you need.

    let cookieMap = document.cookie.split(";").map((str)=>str.split("=")).reduce(function(map,curr){map[curr[0]]=curr[1];return map},{})
    

    You can checkout https://stackoverflow.com/a/59994987/7810354 for an explanation of how this works.

    0 讨论(0)
  • 2020-12-21 15:02

    You could use a javascript library, e.g. jQuery in addition with a plugin: http://plugins.jquery.com/project/cookie

    Demo page: http://stilbuero.de/jquery/cookie/

    0 讨论(0)
  • 2020-12-21 15:02

    try document.cookie. Here is a good link

    0 讨论(0)
提交回复
热议问题