使用 JS Cookie 在 x 页面视图后显示模态?
Display a modal after x pages views with JS Cookie?
我正在尝试显示在我的网站上查看的 Bootstrap 模态 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);
}
你能帮帮我吗?
谢谢
$(document).ready(function() {
// create cookie
var visited = $.cookie('visited'); // visited = 0
if (visited >= 3) {
setTimeout(function() {
$('#my_modal').modal('show'); //make sure model is already created with id my_model
}, 1000);
} else {
visited++; // increase counter of visits
// set new cookie value to match visits
$.cookie('visited', visited, {
expires: 1 // expires after one day
});
return false;
}
});
Make sure all dependent js are included.
jQuery插件在这里
https://github.com/carhartl/jquery-cookie
您可以使用这三个简单的函数来执行 cookie 任务,不需要第三方库。
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);
}
我正在尝试显示在我的网站上查看的 Bootstrap 模态 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);
}
你能帮帮我吗? 谢谢
$(document).ready(function() {
// create cookie
var visited = $.cookie('visited'); // visited = 0
if (visited >= 3) {
setTimeout(function() {
$('#my_modal').modal('show'); //make sure model is already created with id my_model
}, 1000);
} else {
visited++; // increase counter of visits
// set new cookie value to match visits
$.cookie('visited', visited, {
expires: 1 // expires after one day
});
return false;
}
});
Make sure all dependent js are included.
jQuery插件在这里 https://github.com/carhartl/jquery-cookie
您可以使用这三个简单的函数来执行 cookie 任务,不需要第三方库。
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);
}