cookie_name = "Counter_Cookie";
function doCookie() {
if(document.cookie) {
index = document.cookie.indexOf(cookie_name);
}
else {
index = -1;
alert("Welcome the site! Please don't forget to bookmark this page!");
}
var expires = new Date();
expires.setFullYear(expires.getFullYear() + 1);
if (index == -1) {
document.cookie = cookie_name + "=1; expires=" + expires.toUTCString();
}
else {
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
count = eval(document.cookie.substring(countbegin, countend)) + 1;
document.cookie=cookie_name+"="+count+"; expires=" + expires.toUTCString();
}
document.write("<p>You have been to my site "+getTimes()+".</p>");
}
function getTimes() {
if(document.cookie) {
index = document.cookie.indexOf(cookie_name);
if (index != -1) {
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
count = document.cookie.substring(countbegin, countend);
if (count == 1) {
return (count+" time");
}
else {
return (count+" times");
}
}
}
return ("0 times");
}
我想知道如何获取最后一个Cookie到期日期,以显示该用户最后一次访问该网站。我假设我需要一个阵列,但我似乎无法通过我编写的代码了解如何做到这一点。
答案 0 :(得分:0)
微软表示:&#34;浏览器负责管理cookie,cookie的过期时间和日期有助于浏览器管理其cookie存储。因此,虽然您可以阅读cookie的名称和价值,但您无法阅读cookie的到期日期和时间。当浏览器向服务器发送cookie信息时,浏览器不包含过期信息。&#34; http://msdn.microsoft.com/en-us/library/ms178194(v=vs.100).aspx
因此,您可以将带有分隔符的expire
时间戳附加到Cookie的值中:count + '|' + expires.getTime()
然后在getTimes()
中解析时间:
data = document.cookie.substring(countbegin, countend).split('|');
count = data[0];
expirationDate = new Date();
expirationDate.setTime(data[1]);
整个代码:
cookie_name = "Counter_Cookie";
function doCookie() {
if(document.cookie) {
index = document.cookie.indexOf(cookie_name);
}
else {
index = -1;
alert("Welcome the site! Please don't forget to bookmark this page!");
}
var expires = new Date();
expires.setFullYear(expires.getFullYear() + 1);
if (index == -1) {
document.cookie = cookie_name + "=1|" + expires.getTime() + "; expires=" + expires.toUTCString();
}
else {
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
count = eval(document.cookie.substring(countbegin, countend)) + 1;
document.cookie=cookie_name+"="+count+"|" + expires.getTime() + "; expires=" + expires.toUTCString();
}
document.write("<p>You have been to my site "+getTimes()+".</p>");
}
function getTimes() {
if(document.cookie) {
index = document.cookie.indexOf(cookie_name);
if (index != -1) {
countbegin = (document.cookie.indexOf("=", index) + 1);
countend = document.cookie.indexOf(";", index);
if (countend == -1) {
countend = document.cookie.length;
}
data = document.cookie.substring(countbegin, countend).split('|');
count = data[0];
date = new Date();
date.setTime(data[1]);
if (count == 1) {
message = count+" time";
}
else {
message = count+" times";
}
return message + ", last expire: " + date.toUTCString();
}
}
return ("0 times");
}