Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

237
Views
Javascript getCookie function doesn't work properly

I'm simply trying to read a cookie's value, but I seem to be doing something wrong, but I don't see the problem. The cookie does get properly stored and can be accessed under document.cookie, so that's not the problem. Here's my JS code:

function getCookie(name) {  
  const value = `; ${document.cookie}`;  
  const parts = value.split(`; ${name}=`);  
  if (parts.length === 2) return parts.pop().split(';').shift();  
}  
var value = getCookie(value);  
console.log(value);  
console.log(document.cookie);  
  
d = new Date('20 Oct 2022')  
function setCookie() {  
    document.cookie = `value=10000; expires=${d.toUTCString()}`;  
    console.log("cookie set!");  
}

edit: I found my problem. I was missing the quotation marks… I've never felt this stupid :D

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

You can use this function:

function getCookie(cname) {
    let name = cname + "=";
    let decodedCookie = decodeURIComponent(document.cookie);
    let ca = decodedCookie.split(';');
    for (let i = 0; i < ca.length; i++) {
        let c = ca[i];
        while (c.charAt(0) === ' ') {
            c = c.substring(1);
        }
        if (c.indexOf(name) === 0) {
            return c.substring(name.length, c.length);
        }
    }
    return "";
}
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!