如何在javascript中实现createCookie函数

如何在javascript中实现createCookie函数,javascript,jquery,facebook,html,popup,Javascript,Jquery,Facebook,Html,Popup,我有一个脚本(一个倒计时计时器,当达到零时关闭“#fb popupdiv”div): 有什么办法可以和这个结合起来吗 function createCookie(name,value,days) { if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); var expires = "; expires="+date.toGM

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

有什么办法可以和这个结合起来吗

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);
}​
这是可能的,还是我做错了

对不起,我是新手

提前谢谢

请原谅我的英语不好。

你考虑过这个插件吗

请参阅插件:

然后,您可以执行以下操作:

$.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).
});

听起来你在问更多关于如何使用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
    }
});

谢谢你的回答@Wing Lian。不,我没有试过;我以前看到过,但我不知道如何实现它。这就是为什么我尝试使用createCookie函数,但是运气不好。谢谢你的编辑,谢赫。我刚才就想出来了。谢谢你,帕拉什,我会在几分钟后检查出来。谢谢你回答@jfriend00,但问题是,显示弹出窗口的代码是HTML,而不是JavaScript,所以可能很难实现这一点。我是否应该在另一个答案中发布整个代码(CSS、HTML、Javascript)?或者可能我没有正确地跟踪你…@user1903782-在客户端检测cookie是用javascript完成的,因此如果你在客户端上执行cookie,你必须通过javascript来执行你想做的事情。您可以使用javascript为弹出窗口创建HTML。或者,您可以让HTML已经在页面中(默认为隐藏),并用javascript显示它。“弹出窗口”是一个非常通用的术语,如果没有更多关于您试图实现的确切HTML外观的细节,我们就无法提供更好的建议。向我们展示您试图实现的HTML,我们可以更具体。完成。请看手术。再次感谢您抽出时间。使用所有这些代码,我可以在倒计时计时器达到零时使div“#fb popupdiv”关闭。它工作得很好,但每次都会加载,我要寻找的是让它每天为访问者显示一次的任何方法,这样他/她就不会在每次重新加载站点时都被轰炸。@user1903782-我在回答中添加了更多细节,说明如何使弹出窗口每天只显示一次。再次感谢您的回答,但似乎仍然不起作用,也许我犯了一些错误。这就是我所做的:1)将#fb popupdiv的“display:block;”替换为“display:none;”。2) 在OP(倒计时计时器)中的第一个脚本之后添加了介于“”之间的javascript。但是现在,div根本没有出现(重新加载我的IP以进行验证)。
$.cookie("test", 1, { expires : 10 });
$.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).
});
#fb-popupdiv {display: none;}
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
    }
});