Reputation: 781
I am not a programmer. I am trying to use a cookie script that remembers the last drop down menu selection.
I found a script that works but it does only a session cookie. How do I add an expiration date to the cookie in this script?
<head>
<script>
function SETcookie() {
document.cookie = "Selected=" + document.getElementById('myList').selectedIndex;
}
function GETcookie() {
if (document.cookie) {
eval(document.cookie);
document.getElementById('myList').selectedIndex = Selected;
}
}
</script>
</head>
<body onLoad="GETcookie()">
<select id="myList" onChange="SETcookie()">
<option value="1">Option 1</option>
<option value="2">Option 2</option>
<option value="3">Option 3</option>
<option value="4">Option 4</option>
</select>
</body>
Upvotes: 21
Views: 81870
Reputation: 29
This is for setting the expiry date in terms of minutes(5 minutes here)
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + exdays * 60 * 1000);
var expires = "expires="+d.toUTCString();
}
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) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
//your code goes here
} else {
//your code goes here
if (user != "" && user != null) {
setCookie("username", user, 5);
}
}
}
Upvotes: 0
Reputation: 29
This is for setting the expiry date in terms of days(5 days here)
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
var expires = "expires="+d.toUTCString();
}
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) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
function checkCookie() {
var user = getCookie("username");
if (user != "") {
//your code goes here
} else {
//your code goes here
if (user != "" && user != null) {
setCookie("username", user, 5);
}
}
}
Upvotes: 0
Reputation: 8063
;max-age=max-age-in-seconds (e.g., 606024*365 or 31536000 for a year) ;expires=date-in-GMTString-format If neither expires nor max-age specified it will expire at the end of session.
document.cookie = "doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; SameSite=None; Secure";
Upvotes: 1
Reputation: 290
May be this will help
document.cookie = "coolName"+ "=" +"coolValue"+ ";" + "expires="+ new Date(new Date().getTime()+60*60*1000*24).toGMTString()+";path=/";
Upvotes: 9
Reputation: 4211
Try this:
function setCookie(c_name,c_value,exdays) {
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
document.cookie=encodeURIComponent(c_name)
+ "=" + encodeURIComponent(c_value)
+ (!exdays ? "" : "; expires="+exdate.toUTCString());
;
}
c_name
is the name of the cookie
c_value
is the cookie value
exdays
is the number of days you want the cookie to expire after
Upvotes: 16
Reputation: 26413
Here's function which is 100% working and has no depreciated functions.
function setCookie(variable, value, expires_seconds) {
var d = new Date();
d = new Date(d.getTime() + 1000 * expires_seconds);
document.cookie = variable + '=' + value + '; expires=' + d.toGMTString() + ';';
}
Upvotes: 8
Reputation: 37576
try
var a = new Date();
a = new Date(a.getTime() +1000*60*60*24*365);
document.cookie = 'mycookie=somevalue; expires='+a.toGMTString()+';';
PS. The value 1000*60*60*24*365 = 1 Year
To Get the selected index try this GETcookie:
function GETcookie(){
if (document.cookie){
var a = document.cookie;
Selected = a.substring(a.search('Selected=')+9,a.search(';'));
alert("Selected = " + Selected);
document.getElementById('myList').selectedIndex=Selected;
}}
Upvotes: 6
Reputation: 1952
You could try this:
function SETcookie(){
var validity_days = 7;
var expires = validity_days * 1000 * 60 * 60 * 24;
var expires_date = new Date( today.getTime() + (expires) );
document.cookie="Selected="+document.getElementById('myList').selectedIndex + ";expires=" + expires_date.toGMTString() + ";";
}
Upvotes: 0