show cookies data to table

Viewed 30

I have a web that shows 2 prompts to input name and password, then it will store those data to cookies. My question is how to make these data display in table?

In the table I want to display cookies data:

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() {
  let user = getCookie("username");
  let password = getCookie("password");
  if (user != "" && password != "") {
    alert("Welcome again " + user);
  } else {
    user = prompt("Please enter your name:", "");
    password = prompt("Please enter your password:", "");
    if (user != "" && user != null && password != "" && password != null) {
      setCookie("username", user, 30);
      setCookie("password", password, 30);
    }
  }
}
<body onload="checkCookie()">
  <table id="cookiesTable" style="border:1px solid;">
    <thead>
      <tr>
        <th>NAME</th>
        <th>PASSWORD</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td onclick="checkCookie()">cookies name</td>
        <td onclick="checkCookie()">cookies password</td>

      </tr>
    </tbody>
  </table>
</body>

0 Answers
Related