我想每天使用cookie显示一次弹出框 我用来显示tinybox popup的是
<script type="text/javascript">
window.onload = function()
{
TINY.box.show({html:'Hello',autohide:20,width:400,height:320});
document.getElementsByClassName('tmask')[0].onclick=null;
}
</script>
答案 0 :(得分:2)
使用此代码。我测试过&amp;它很完美。
<html>
<head>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.7.0/jquery.min.js"></script>
<script type="text/javascript" src="https://github.com/carhartl/jquery-cookie/raw/master/jquery.cookie.js"></script>
<script type="text/javascript">
$(document).ready(function() {
if( $.cookie('showOnlyOne') ){
//it is still within the day
//hide the div
$('#shownOnlyOnceADay').hide();
} else {
//either cookie already expired, or user never visit the site
//create the cookie
$.cookie('showOnlyOne', 'showOnlyOne', { expires: 1 });
//and display the div
$('#shownOnlyOnceADay').show();
}
});
</script>
</head>
<body>
<div id="shownOnlyOnceADay">
This text should only be shown once a day!
</div>
</body>
</html>
通过更改系统日期对其进行测试。
答案 1 :(得分:0)
您可以添加像这样的Cookie
if(getCookie("showed")!="1"){
TINY.box.show({html:'Hello',autohide:20,width:400,height:320});
document.getElementsByClassName('tmask')[0].onclick=null;
setCookie('showed','1','1')"
}
function getCookie(c_name)
{
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1)
{
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1)
{
c_value = null;
}
else
{
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1)
{
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start,c_end));
}
return c_value;
}
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
答案 2 :(得分:0)
试试这个
$(document).ready(function () {
function getCookie(c_name) {
var c_value = document.cookie;
var c_start = c_value.indexOf(" " + c_name + "=");
if (c_start == -1) {
c_start = c_value.indexOf(c_name + "=");
}
if (c_start == -1) {
c_value = null;
} else {
c_start = c_value.indexOf("=", c_start) + 1;
var c_end = c_value.indexOf(";", c_start);
if (c_end == -1) {
c_end = c_value.length;
}
c_value = unescape(c_value.substring(c_start, c_end));
}
return c_value;
}
function setCookie(c_name, value) {
var c_value = escape(value);
document.cookie = c_name + "=" + c_value;
}
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;
}
if (checkCookie()) {
TINY.box.show({
html: 'Hello',
autohide: 20,
width: 400,
height: 320
});
document.getElementsByClassName('tmask')[0].onclick = null;
setCookie("showed", new Date());
}
});