-2

I want KL_Key value. I have tried with below method but did'nt get its value. What I doing wrong. Any easiest way to get KL_Key value

Sample Cookie:

KL_Success=YES; KL_ID=473CA3E9-91F3-; KL_Key:32305bcc-34a9-4500;

Method to getcookie value:

function getCookie() {
  const result = decodeURIComponent(document.cookie);
  console.log(result)
  return (result === null) ? null : result[1];
}

console.log(getCookie("KL_Key"));

I want to get KL_Key value 32305bcc-34a9-4500 in return.

Thanks in advance.

  • 1
    Possible duplicate of [Get cookie by name](https://stackoverflow.com/questions/10730362/get-cookie-by-name) – jtate Mar 12 '19 at 14:26

1 Answers1

0

I'm giving you a step by step code of how it can be extracted

// Split cookies string into individual cookies
var cookies = document.cookie.split('; ')
// Return as a cookie object
cookies.map(cookie => {
   // Split by = so that we can get the key-value pair
    var keyValue = cookie.split('=')
    var key = keyValue[0]
    var value = keyValue[1]
   // Create a new object with the key value pair
    var obj = {}
    obj[key] = value
    return obj
})
AshTyson
  • 473
  • 7
  • 23