创建Cookie Javascript [英] Create cookie Javascript

查看:42
本文介绍了创建Cookie Javascript的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当用户单击按钮时,我必须创建一个JS cookie,该cookie将在10分钟后出现另一个弹出窗口,以记住他.

I have to create a JS cookie when a user click a button, this cookie will remember him after 10 minutes with another popup.

示例:

<button>Click me!</button>

,当用户单击该按钮时,该按钮将隐藏.10分钟后,该按钮将再次显示:

and the button will hide when user click, after 10 minutes the button will show again:

<button>Click me!</button>

脚本部分:

function setCookieMsg(name) {
        var d = new Date();
        var time = d.setTime(d.getTime() + (600000));
        document.cookie = name + "=" + time;
    }

    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) != -1) return c.substring(name.length, c.length);
    }
    return "";
    }

    function checkCookieMsg() {
    var cookie = getCookie("name");
    var d = new Date() - 600000;
    if (cookie == "") {
        setCookie("name", cookie);
    }else if (d => cookie) {
        $().getUnReadMessage();
        }
    }

我怎么了?

推荐答案

您没有在cookie中提及过期时间.到期日期应为UTC时间字符串.

You didn't mention the expires in cookie. The expires date should be UTC time string.

function setCookieMsg(name) {
     var date = new Date();
     date.setTime(date.getTime()+(600000));
     var expires = "; expires="+date.toUTCString();
     document.cookie = name + "=" + expires;
}

这篇关于创建Cookie Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆