[英]set cookie on page to show bootstrap popup once a day
我正在学习JavaScript,看到这个问题已经问了很多遍了,但是我无法解决这个问题。 我想做的是,每天显示一次引导程序模式。
到目前为止,我有:
function setCookie(cookiename, cookievalue, expdays) {
var d = new Date();
d.setTime(d.getTime()+(expdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cookiename + "=" + cookievalue + "; " + expires;
}
function getCookie(cookiename) {
var name = cookiename + "=";
var ca = document.cookie.split(';');
for(var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
if (c.indexOf(name) == 0) return c.substring(name.length, c.length);
}
//I want to check if there is a cookie.
//if I have not set a cookie, I want to show my modal,
//if there is a cookie then return;
//The cookie should expire in one day.
function checkCookie() {
var showed = getCookie("showed");
if (showed != null && showed != "") {
var date = new Date(showed).getDate();
var currentDate = new Date().getDate();
if (currentDate > date) {
return true;
}
return false;
}
return true;
}
现在,如果我将最后一个返回值更改为true; 返回假 我的模态没有显示。 现在,我每次都看到模态。 我究竟做错了什么? 我怎样才能解决这个问题?
function setCookie(cookiename, cookievalue, expdays) { var d = new Date(); d.setTime(d.getTime()+(expdays * 24 * 60 * 60 * 1000)); var expires = "expires=" + d.toGMTString(); document.cookie = cookiename + "=" + cookievalue + "; " + expires; } function getCookie(cookiename) { var name = cookiename + "="; var startPos = document.cookie.indexOf(name); if(startPos == -1) return null; startPos+=(name.length); if(document.cookie.indexOf(";",startPos) == -1){ return document.cookie.substring(startPos,document.cookie.length); } else{ return document.cookie.substring(startPos,document.cookie.indexOf(';',startPos)); } return null; } //I want to check if there is a cookie. //if I have not set a cookie, I want to show my modal, //if there is a cookie then return; //The cookie should expire in one day. function checkCookie() { var showed = getCookie("showed"); if (showed != null && showed != "") { var date = new Date(showed).getDate(); var currentDate = new Date().getDate(); if (currentDate > date) { return true; } return false; } return true; }
另外,在设置Cookie时,请使用
setCookie('showed',new Date().toGMTString(),1);
因为我们使用cookie的值,而不是要检查cookie的过期时间。 因此该值必须是日期字符串
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.