【发布时间】:2019-08-23 23:00:54
【问题描述】:
我正在尝试显示在我的网站上查看的 Bootstrap modal avec 3 页面,并使用 JS-Cookie 插件找到this 问题的实际解决方案。
我试过这段代码,但它不起作用:
$(document).ready(function () {
// create cookie
var visited = Cookies('visited'); // visited = 0
if (visited >= 3) {
// open fancybox after 3 secs on 4th visit or further on the same day
$('#my_modal').modal('show');
} else {
visited++; // increase counter of visits
// set new cookie value to match visits
Cookies('visited', visited, {
expires: 1 // expires after one day
});
return false;
}
}); // ready
上面的代码应该做什么:在 4 页重新加载后提示模式,like here
什么不工作:我重新加载页面但模式没有出现
这是我的 cookie 状态(在 chrome 上): Image on this link
这是一个实际用于在网站首次访问时显示模式的代码示例:
$(document).ready(function() {
if (Cookies('pop_welcome') == null) {
$('#my_modal').modal('show');
Cookies('pop_welcome', '31', { expires: 31 });
}
});
** -- 解决方案 -- **
您可以找到解决方案into @greedchikara code snip
代码:
function createCookie(name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
var nameEQ = name + "=";
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,c.length);
if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
}
return null;
}
function eraseCookie(name) {
createCookie(name,"",-1);
}
var visited = readCookie('tester') || 1;
if (visited > 2) {
$('#my_modal').modal('show');
} else {
visited++;
createCookie('tester', visited, 1);
}
你能帮帮我吗? 谢谢
【问题讨论】:
-
检查控制台是否有错误。请注意,
Cookies不是标准方法,因此除非您自己添加,否则会导致错误 -
感谢您的回答。此代码不会在我的 chrome 控制台上提示任何错误...我使用 Cookies 方法的工作代码片段编辑了我的问题
-
检查
$('#my_modal').modal('show');是否从控制台打开模态 -
那么
Cookies('visited')会返回什么?您显示它在 cmets 中返回0。做一些日志记录到控制台,看看发生了什么 -
@greedchikara cookie 没有打开模式。 cookie 存在但没有做任何事情 :'(
标签: javascript jquery cookies js-cookie