2012-12-14 48 views
3

所以我有这个脚本(倒数计时器,关闭“#FB-popupdiv” DIV,当达到零):如何实现功能的createCookie在JavaScript

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:对不起,我可怜的英文不好。

+0

感谢您使的编辑,谢赫。我刚才知道了。 – user1903782

回答

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 
    } 
}); 
+0

谢谢你回答@ jfriend00,但是这里的东西,显示弹出窗口的代码是HTML,而不是JavaScript,所以也许很难做到这一点。我应该在另一个答案中发布整个代码(CSS,HTML,Javascript)吗?或者,也许我没有正确地关注你... – user1903782

+0

@ user1903782 - 在客户端检测cookie是使用javascript完成的,因此您将需要通过JavaScript执行操作,以便在您执行操作时执行您想要的操作在客户端。您可以使用javascript为弹出窗口创建HTML。或者,您可以让HTML中已存在的HTML(默认为隐藏)并使用javascript显示。 “Popup”就是这样一个通用术语,如果没有关于你想要做什么确切的HTML外观的更多细节,我们不能提供更好的建议。向我们展示您试图实现的HTML,并且我们可以更具体。 – jfriend00

+0

完成。请参阅OP。并再次感谢您的时间。有了所有的代码,我可以使div'#fb-popupdiv'在倒数计时器达到零时关闭。它运行良好,但它每次都会加载,所以我一直在寻找的方法是让访问者每天出现一次,所以每次重新加载站点时都不会受到轰炸。 – user1903782

1

您是否考虑过jquery.cookie插件?

if (!$.cookie('daily_popup')) { 
    // Code to show modal/popup; e.g. show_modal(); 
    $.cookie('daily_popup', 1, {expires: 1}); 
} 
+0

谢谢你的回答@Wing Lian。不,我没有尝试过;早些时候看到它,但我不知道如何实现它。这就是为什么我尝试使用函数createCookie,但没有运气。 – user1903782

1

看到该插件:

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

那么你可以做:

$.cookie("test", 1); 

删除:

$.cookie("test", null); 

此外,设定的超时然而在特定的对cookie的天数为n(10此处):

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

如果过期选项被忽略,那么饼干变成一个会话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). 
}); 
+0

谢谢Palash,会在几分钟内检查出来。 – user1903782