Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

238
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda