我不是程序员。我正在尝试使用cookie脚本来记住最后一个下拉菜单选项。
我找到了一个有效的脚本,但它只有一个会话cookie。如何在此脚本中向cookie添加过期日期?
<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>
答案 0 :(得分:9)
试试这个:
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
是Cookie的名称
c_value
是Cookie值
exdays
是您希望Cookie在
答案 1 :(得分:5)
可能会有所帮助
[index, value]
[4,1]
[9,2]
答案 2 :(得分:4)
试
var a = new Date();
a = new Date(a.getTime() +1000*60*60*24*365);
document.cookie = 'mycookie=somevalue; expires='+a.toGMTString()+';';
PS。值1000 * 60 * 60 * 24 * 365 = 1年
要获取所选索引,请尝试以下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;
}}
答案 3 :(得分:3)
这里的功能100%正常,没有折旧功能。
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() + ';';
}
答案 4 :(得分:0)
你可以试试这个:
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() + ";";
}
答案 5 :(得分:0)
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);
}
}
}
答案 6 :(得分:0)
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);
}
}
}