- Forums
- Javascipt
- Simple Javascript Set Cookie and Get Cookie functions to Store Cookie Values
These are two simple functions you can use in your javascript to manage cookies for your pages using a get and name cookies. [4963], Last Updated: Mon Jun 24, 2024
edwin
Fri Nov 05, 2021
0 Comments
601 Visits
I want to share two very important functions that work on my javascript that you can use. credit goes to the link included for calling a cookie and setting a cookie value.
//https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/Set-Cookie/SameSite
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
var expires = "expires=" + d.toUTCString();
document.cookie =
cname + "=" + cvalue + "; SameSite=None; secure; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(";");
for (var i = 0; i < ca.length; i++) {
var c = ca[i];
while (c.charAt(0) == " ") c = c.substring(1);
if (c.indexOf(name) != -1) return c.substring(name.length, c.length);
}
return "";
}