The cookie framework is commonly used to store data longer than one browser session, but users can delete, block and change cookies using the browser settings. JavaScript Cookies are not very good and should not be used. A good alternative will be suggested later, web storage.
See the below links for how to use the cookie framework (only as a backup):
You can implement cookies using the following code:
if (navigator.cookieEnabled) {
document.cookie = "cookie_name = cookie_value;expires = expiry";//how to set a cookie
var name = cookie_name + "=", 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) == 0) {varresult = c.substring(name.length, c.length);}
}//how to get a cookie
}
where cookie_name
is the name of the cookie you want to use, cookie_value
is the value of your cookie you want to store, expiry
is the date that you can access the cookie until (expressed in milliseconds from January 1, 1970), result
is the value of the cookie that you want to get
You can remove a cookie by redeclaring the cookie, but putting the expiry date in the past.