Don't ask me why, but I always encountered problems by managing cookies using any kind of plugin most often your problems are around the plugin not loaded or a jQuery version mismatch, as extra the $.cookie
plugin is no longer maintained now, so likely you are encurring in a bug.
So, document.cookie
is your friend, not only because you don't need any libraries for that, but even because it's more semantic than the jQuery counterpart.
In your example you might want to do something like this, to set the expiration in the next 10 days.
const expiration = new Date();
expiration.setDate(expiration.getDate() + 10);
expiration.toUTCString();
document.cookie = `NAME=VALUE; expires=${expiration}; path=/`;
You can slap this in a function so you can call that multiple times, if you need to set up different cookies.
const setcookie = (name: string = "", value: string = "", days: number = 1) => {
if (name === "") {
throw new Error("Cookie name cannot be empty");
}
const expiration = new Date();
expiration.setDate(expiration.getDate() + days);
expiration.toUTCString();
document.cookie = `${name}=${value}; expires=${expiration}; path=/`;
}
setcookie("NAME", "VALUE", 10);