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

176
Views
I tried to make a name stored into a cookie, but it stopped working

I was trying to make a page where we can chat and it will store its user's name to a cookie on the page. I tried it for a couple times, it worked. Then suddenly the cookie part doesn't work anymore. I am hosting the code on replit. What is the problem?

This is my code:

<!DOCTYPE html>
<html>
<head>
<script>
function setCookie(cname,cvalue,exdays) {
  const d = new Date();
  d.setTime(d.getTime() + (exdays*24*60*60*1000));
  let expires = "expires=" + d.toUTCString();
  document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

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 "";
}

function checkCookie() {
  user = prompt("Please enter your name (your name wont be on the internet, use your real name. this is a  school chat):","");
  if (user != "" && user != null) {
    setCookie("name", user, 30);
    let user = getCookie("name");
    document.getElementById("name").value = user;
  }
  else {
    checkCookie()
  }
}

function checkname() {
  let user = getCookie("name");
  if (user == "" && user == null) {
    checkCookie()
  }
  else{
    document.getElementById("welcome").innerHTML = "Welcome Back " + user;
    document.getElementById("name").value = user;
  }
}
</script>
</head>
<body onload=checkname()></body>
<p id="welcome"></p>
<form action="https://online-chat-2.red78massive157.repl.co" method="post">
    Name: <br><input name="name" type="text" id="name" readonly/><br>
    Message: <br><input name="message" type="text" style='height:100px; width:1000px' required/><br>
    <input value="Send" type="submit"/>
</form>
<button onclick=checkCookie()>Change name</button>
<br><br>
<iframe src="https://online-chat-2.red78massive157.repl.co/chat" style='height:1000px; width:500px'/>

</html>
about 4 years ago · Santiago Trujillo
1 answers
Answer question

0

I am not really sure whats not working for you but I found that checkCookie function has an issue. You are trying to use variable user before initialization.

Rewrite code as

function checkCookie() {
  const user = prompt("Please enter your name (your name wont be on the internet, use your real name. this is a  school chat):","");
  if (user != "" && user != null) {
    setCookie("name", user, 30);
    document.getElementById("name").value = getCookie("name");
  }
  else {
    checkCookie()
  }
}
about 4 years ago · Santiago Trujillo 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!