3

所以我有这个脚本(一个倒数计时器,当达到零时关闭“#fb-popupdiv”div):

var dom = {};
dom.query = jQuery.noConflict(true);
var time = 11;
window.setInterval(test, 1000);
function test()
{
    time -=1;
    dom.query('#my_timer').html(time); 
    if(time == 0)        
    {
        dom.query('#fb-popupdiv').remove();
    }
} 

有什么办法可以结合这个吗?:

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);
}​

让我解释。我正在制作一个likebox popup div,我希望访问我网站的用户在一天内只看到一次弹出窗口(因此,他们不会一次又一次地访问我的网站,多次看到它)。所以我试图想办法结合这两个脚本来实现这一点。

这是我到目前为止所得到的:

HTML:

<div id="fb-popupdiv">
<div id="fb-popup">
<h1 class="fb-title">To continue, click "Like" button</h1>
<p style="background:#fff;padding-bottom:20px;">
<iframe src="//www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2FBlindGuardianArgentina&amp;width=457&amp;height=263&amp;show_faces=true&amp;colorscheme=light&amp;stream=false&amp;border_color=%23fff&amp;header=false" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:457px; height:263px;" allowTransparency="true"></iframe>
<span class="fb-closebutton">Or wait <span id="my_timer"></span> seconds.</span>
</p>
</div>
</div>

CSS:

#popupdiv{position:absolute;display:none;}
#fb-popupdiv{display:block;top:180px;left:278.5px;width:500px;height:355px;position:fixed;background-image:url('http://i.imgur.com/IHT1l.png');margin:0;overflow-y:auto;z-index:999999;text-align:center;border:10px solid rgba(82, 82, 82, .7);border-radius:8px;}
#fb-popup{background-color:#fff;overflow:none;z-index:999999;height:227px;}
.fb-title{background:#6D84B4 none repeat scroll 0 0;border-top:1px solid #3B5998;border-left:1px solid #3B5998;border-right:1px solid #3B5998;color:white !important;padding:5px !important;margin:0 !important;font:normal 14px "Lucida Sans Unicode", "Lucida Grande", sans-serif !important;}
.fb-closebutton{float:right;font:normal 11px/2.5em Arial, sans-serif !important;padding:5px 15px 20px 0;background:#fff;width:97%;text-align:right;color:#000 !important;}
#my-timer{width:400px;background:#fff; margin:0 auto;padding:5px 0px 5px 0px;}

这是可能的,还是我做错了?

我是新手,对不起。

提前致谢!

PS:请原谅我糟糕的英语。

4

3 回答 3

1

你考虑过jquery.cookie插件吗?

if (!$.cookie('daily_popup')) {
    // Code to show modal/popup; e.g. show_modal();
    $.cookie('daily_popup', 1, {expires: 1});
}
于 2012-12-14T17:58:23.797 回答
1

查看插件:

https://github.com/carhartl/jquery-cookie

然后你可以这样做:

$.cookie("test", 1);

删除:

$.cookie("test", null);

此外,要在 cookie 上设置一定天数(此处为 10)的超时时间:

$.cookie("test", 1, { expires : 10 });

如果省略 expires 选项,则 cookie 成为会话 cookie,并在浏览器退出时被删除。

涵盖所有选项:

$.cookie("test", 1, {
   expires : 10,           //expires in 10 days

   path    : '/',          //The value of the path attribute of the cookie 
                           //(default: path of page that created the cookie).

   domain  : 'jquery.com',  //The value of the domain attribute of the cookie
                           //(default: domain of page that created the cookie).

   secure  : true          //If set to true the secure attribute of the cookie
                           //will be set and the cookie transmission will
                           //require a secure protocol (defaults to false).
});
于 2012-12-14T18:00:47.510 回答
1

听起来您在询问更多关于如何使用 cookie 代码以便每天只显示一次的信息。这是基本的逻辑。

在你的 CSS 中有这个,所以默认情况下弹出窗口不可见:

#fb-popupdiv {display: none;}

然后,添加这个 javascript:

dom.query(document).ready(function() {
    var val = readCookie("popupAlreadyShown");
    if (!val) {
        createCookie("popupAlreadyShown", 1, 1);
        // use your code here to show the popup here
        dom.query("#fb-popupdiv").show();
    } else {
        // popup was already shown less than 1 day ago
        // because cookie still exists
        // do anything else you might want to do here
    }
});
于 2012-12-14T18:07:47.777 回答