一小时后在javascript中进行销毁会话

时间:2019-05-14 10:26:34

标签: javascript

当用户访问该站点一次仅给出一个小狗表格时,此代码有效,此处的cookie保存在浏览器中,并且当同一用户再次访问该站点时,他不再获取小狗表格,我希望当第二天同一用户访问网站,cookies应该自动清除 12点后(深夜) 这是我的代码

  function PopUp(hideOrshow) {
        if (hideOrshow == 'hide') {
            document.getElementById('ac-wrapper').style.display = "none";
        }
        else  if(localStorage.getItem("popupWasShown") == null) {
            localStorage.setItem("popupWasShown",1);
            document.getElementById('ac-wrapper').removeAttribute('style');
        }
    }
    window.onload = function () {
        setTimeout(function () {
            PopUp('show');
        }, 0);
    }


    function hideNow(e) {
        if (e.target.id == 'ac-wrapper') document.getElementById('ac-wrapper').style.display = 'none';
    }
#ac-wrapper {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background: url("images/pop-bg.png") repeat top left transparent;
    z-index: 1001;
}
#popup {
    background: none repeat scroll 0 0 #FFFFFF;
    border-radius: 18px;
    -moz-border-radius: 18px;
    -webkit-border-radius: 18px;
    height: 361px;
    margin: 5% auto;
    position: relative;
    width: 597px;
}
<div id="ac-wrapper" style='display:none' onClick="hideNow(event)">
    <div id="popup">
        <center>
             <h2>Popup Content Here</h2> 
            <input type="submit" name="submit" value="Submit" onClick="PopUp('hide')" />
        </center>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

因此它会检查cookie是否在窗口onload上存在,然后在单击时将其隐藏,并且cookie会在午夜过期

 function hideNow() {
        document.getElementById('ac-wrapper').style.display = 'none';
        setCookie('show_cookie_message','no');
        return false;
    }
    window.onload = function () {

        if(getCookie('show_cookie_message') != 'no')
        {
            document.getElementById('ac-wrapper').removeAttribute('style');
        }
    }

function setCookie(cookie_name, value)
{
    var now = new Date();
    var expire = new Date();
    expire.setFullYear(now.getFullYear());
    expire.setMonth(now.getMonth());
    expire.setDate(now.getDate()+1);
    expire.setHours(0);
    expire.setMinutes(0);
    document.cookie = cookie_name + "=" + escape(value) + "; expires="+expire.toUTCString() + "; path=/";
}

function getCookie(cookie_name)
{
    if (document.cookie.length>0)
    {
        cookie_start = document.cookie.indexOf(cookie_name + "=");
        if (cookie_start != -1)
        {
            cookie_start = cookie_start + cookie_name.length+1;
            cookie_end = document.cookie.indexOf(";",cookie_start);
            if (cookie_end == -1)
            {
                cookie_end = document.cookie.length;
            }
            return unescape(document.cookie.substring(cookie_start,cookie_end));
        }
    }
    return "";
}
<div id="ac-wrapper" style='display:none' >
    <div id="popup">
        <center>
             <h2>Popup Content Here</h2> 
            <input type="submit" name="submit" value="Submit" onClick="hideNow()" />
        </center>
    </div>
</div>