Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

109
Visualizações
js variable logs perfect in console.log but wont work in create cookie function

I am using an html page builder by unlayer. Once user clicks save button I get the html like this:

document.getElementById("save").addEventListener("click", () => {
        unlayer.exportHtml(function (data) {   
var bl = new Blob([html], { type: "text/plain" });
            var a = document.createElement("a");
            
            a.download = "download-name-here.html";
            a.hidden = true;
            document.body.appendChild(a);
            let blob = a;
            a.href = fetch(a).then(r => r.blob());
            a.click();
            const reader = new FileReader();
          


// This fires after the blob has been read/loaded.
reader.addEventListener('loadend', (e) => {
  const text = e.srcElement.result;
  console.log(text);
  createCookie("myCookieCustom", text, "10");
  
});
  });
      }); 

and when I console.log the var "text" I get the html perfectly but it doesnt save in the createCookie function. I have tested "hello" instead of text in create cookie and that worked. Any idea how to solve this? just in case I am using the following functions to save and set cookies :

function createCookie(name, value, days) {
   var expires;
     
   if (days) {
       var date = new Date();
       date.setTime(date.getTime() + (days * 4 * 1 * 1 * 1000));
       expires = "; expires=" + date.toGMTString();
   }
   else {
       expires = "";
   }
     
   document.cookie = escape(name) + "=" + 
       escape(value) + expires + "; path=/";
}

function getCookie(name) {
   var dc = document.cookie;
   var prefix = name + "=";
   var begin = dc.indexOf("; " + prefix);
   if (begin == -1) {
       begin = dc.indexOf(prefix);
       if (begin != 0) return null;
   }
   else
   {
       begin += 2;
       var end = document.cookie.indexOf(";", begin);
       if (end == -1) {
       end = dc.length;
       }
   }
   // because unescape has been deprecated, replaced with decodeURI
   //return unescape(dc.substring(begin + prefix.length, end));
   return decodeURI(dc.substring(begin + prefix.length, end));
} 

You can view the full html and css here in a w3 snippet I made : https://www.w3schools.com/code/tryit.asp?filename=GUCW5XSUMN1L and I am checking the cookie in php with (dont need help with php that I am fine with)

<?php
    echo $_COOKIE["myCookieCustom"];
?>

Thanks in advance.

about 4 years ago · Juan Pablo Isaza
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda