简体   繁体   中英

Any simplest way to get cookie value in javascript

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

try document.cookie. Here is a good link

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM