如何测试javascript cookie是否已过期?

时间:2012-01-30 23:34:00

标签: javascript jquery

是否可以使用?

测试javascript cookie是否已过期

我需要有条件地做一些事情并且其中两个条件是重叠的,如果可以测试cookie是否已经过期,那么我将更容易完成任务。

我正在使用jquery-1.5.jsjquery.cookies.js插件。

感谢。

CODE

var jq = jQuery.noConflict();
jq(document).ready(function () {

    var timeStart, timeSubmit, timeLeft;
    timeSubmit = 5 * 60 * 1000;
    timeStart = jaaulde.utils.cookies.get("_watchman");
    try {
        if(jaaulde.utils.cookies.test()) {
            throw "err1";
        }
        else if(hasCookieExpired) {
            throw "err2";
        }
        else if(!timeStart) {
            jaaulde.utils.cookies.set("_watchman", String(new Date().getTime()), {path: '/path', expiresAt: new Date((new Date().getTime() + timeSubmit))});
            timeLeft = timeSubmit - (new Date().getTime() - Number(jaaulde.utils.cookies.get("_watchman")));
            timeCheck();
        }
        else {
            timeLeft = timeSubmit - (new Date().getTime() - Number(jaaulde.utils.cookies.get("_tts")));         

            timeCheck();
        }

    } catch(err) {
        //handle errors
    }

    function timeCheck() {
            if(timeLeft <= 0) {
                triggerSubmit();
            }
            else {
                setTimeout(triggerSubmit, timeLeft);    
                setInterval(showTimeLeft, 1000);
            }       
    }

    function triggerSubmit() {
        //submit it
    }

    function showTimeLeft() {
        //do something
    }

});

4 个答案:

答案 0 :(得分:10)

if( $.cookie('yourCookie') === null ) {
    // EXPIRED
} else {
    // DO SOMETHING ELSE
}

或者像这样使用三元运算符

$.cookie('yourCookie') === null ? /* EXPIRED */ : /* DO SOMETHING ELSE */;

https://github.com/carhartl/jquery-cookie(不再维护,存档)
https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie
https://github.com/js-cookie/js-cookie

答案 1 :(得分:1)

浏览器会在过期后自动删除任何cookie,因此您需要做的就是检查cookie是否存在。

答案 2 :(得分:1)

如果您说浏览器没有删除cookie,可能是因为您的时区与UTC不同。

答案 3 :(得分:1)

要测试,请输入以下HTML代码

<label for="expsecs">Cookie expiration in seconds:</label>
<input type="text" id="expsecs" value="5" /><br />
<button id="setcookie">set cookie</button>
<button id="getcookie">get cookie</button>
<div id="cookiestatus">status</div>

请输入以下jQuery代码

function set_cookie() {
    var secs = parseInt($('#expsecs').val(), 10);
    if (isNaN(secs)) {
        secs = 5;
    }
    var now = new Date();
    var exp = new Date(now.getTime() + secs*1000);
    var status = '';
    document.cookie = 'ExpirationCookieTest=1; expires='+exp.toUTCString();
    if (document.cookie && document.cookie.indexOf('ExpirationCookieTest=1') != -1) {
        status = 'Cookie successfully set. Expiration in '+secs+' seconds';
    } else {
        status = 'Cookie NOT set. Please make sure your browser is accepting cookies';
    }

    $('#cookiestatus').text(status);
}

function get_cookie() {
    var status = '';
    if (document.cookie && document.cookie.indexOf('ExpirationCookieTest=1') != -1) {
        status = 'Cookie is present';
    } else {
        status = 'Cookie is NOT present. It may be expired, or never set';
    }
    $('#cookiestatus').text(status);
}

function init() {
    $('#setcookie').bind('click', set_cookie);
    $('#getcookie').bind('click', get_cookie);
}

$(init);

jsfiddle

中查看此内容