我正在尝试创建一个基于Cookie的重定向,一旦通过4天就会将某人带到新页面。
我希望我的网站访问者在前10天内至少访问促销优惠页面7次,因此我需要确保Cookie生成至少30天,并且计时器在首次访问时动态启动。
现在我只有这个代码,希望得到一些帮助!
<script>
$(document).ready(function() {
if (getCookie('cookiesSet') == "") {
setCookie('cookiesSet', 'yes', 365);
setCookie('passed4days', 'no', 4);
} else if (getCookie('passed4days') == '')
window.location = 'www.facebook.com';
});
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires="+d.toUTCString();
document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}
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 "";
}
</script>
&#13;
答案 0 :(得分:0)
预期的片段样式:
$(document).ready(function () {
var firstVisitTime = getCookie('firstVisitTime')
if (firstVisitTime != '') {
var soManyDays = ((new Date()).getTime() - firstVisitTime) / (24 * 60 * 60 * 1000)
if (soManyDays > 4) window.location = 'www.stackoverflow.com'
else if (soManyDays > 24) console.log('I should remember you')
} else setCookie('firstVisitTime', (new Date()).getTime(), 366)
})