javascripthtmldatabasecookies

Can I use cookies for a standard HTML file?


I tried to pass a cookie to remember the user's login details:

const expiresIn24Hours = new Date(Date.now() + 24 * 60 * 60 * 1000).toUTCString(); 
    document.cookie = `uuid=${userId}; expires=${expiresIn24Hours}; path=/; `;
    document.cookie = `firstName=${firstName}; expires=${expiresIn24Hours}; path=/; `;

When I use an HTML file with this code and console.log the cookie, it shows nothing.

However, when I run it in Live Server for VS Code, the cookie displays properly:

uuid=[MyUUID]; firstName=[MyFirstName]

Can a cookie be implemented in a .html file?

Do you need to have a server for it?


Solution

  • As I remember Cookie require 'HTTP' server. The answer is YES, To use cookie you will need an HTTP server.