开发者

Javascript equivalent of PHP $_COOKIE["thecookie"] [duplicate]

开发者 https://www.devze.com 2023-03-15 03:49 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_开发技巧 Possible Duplicate: Any simplest way to get cookie value in javascript
This question already has answers here: Closed 11 years ago. 开发者_开发技巧

Possible Duplicate:

Any simplest way to get cookie value in javascript

Is there a javascript equivalent to the PHP $_COOKIE["thecookie"] to get the value of a cookie by name? Also, is there a javascript equivalent to the PHP functions isset() or empty() to test the value of the cookie?

Thanks.


Here are a few functions for js cookie operations:

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

All functions are from this site

0

精彩评论

暂无评论...
验证码 换一张
取 消