Read a javascript cookie by name

后端 未结 11 979
盖世英雄少女心
盖世英雄少女心 2020-12-05 06:50

I have set a cookie using

document.cookie = 
    \'MYBIGCOOKIE=\' + value + 
    \'; expires=\' + now.toGMTString() + 
    \'; path=/\';

No

相关标签:
11条回答
  • 2020-12-05 07:26

    Here is an example implementation, which would make this process seamless (Borrowed from AngularJs)

    var CookieReader = (function(){
    var lastCookies = {};
    var lastCookieString = '';
    
    function safeGetCookie() {
        try {
            return document.cookie || '';
        } catch (e) {
            return '';
        }
    }
    
    function safeDecodeURIComponent(str) {
        try {
            return decodeURIComponent(str);
        } catch (e) {
            return str;
        }
    }
    
    function isUndefined(value) {
        return typeof value === 'undefined';
    }
    
    return function () {
        var cookieArray, cookie, i, index, name;
        var currentCookieString = safeGetCookie();
    
        if (currentCookieString !== lastCookieString) {
            lastCookieString = currentCookieString;
            cookieArray = lastCookieString.split('; ');
            lastCookies = {};
    
            for (i = 0; i < cookieArray.length; i++) {
                cookie = cookieArray[i];
                index = cookie.indexOf('=');
                if (index > 0) { //ignore nameless cookies
                    name = safeDecodeURIComponent(cookie.substring(0, index));
    
                    if (isUndefined(lastCookies[name])) {
                        lastCookies[name] = safeDecodeURIComponent(cookie.substring(index + 1));
                    }
                }
            }
        }
        return lastCookies;
    };
    })();
    
    0 讨论(0)
  • 2020-12-05 07:32

    using jquery-cookie

    I find this library helpful. 3.128 kb of pure convenience.

    add script

    <script src="/path/to/jquery.cookie.js"></script>
    

    set cookie

    $.cookie('name', 'value');
    

    read cookie

    $.cookie('name');
    
    0 讨论(0)
  • 2020-12-05 07:36

    Here is an API which was written to smooth over the nasty browser cookie "API"

    http://code.google.com/p/cookies/

    0 讨论(0)
  • 2020-12-05 07:37
    function getCookie(c_name)
    {
        var i,x,y,ARRcookies=document.cookie.split(";");
    
        for (i=0;i<ARRcookies.length;i++)
        {
            x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
            y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
            x=x.replace(/^\s+|\s+$/g,"");
            if (x==c_name)
            {
                return unescape(y);
            }
         }
    }
    

    Source: W3Schools

    Edit: as @zcrar70 noted, the above code is incorrect, please see the following answer Javascript getCookie functions

    0 讨论(0)
  • 2020-12-05 07:41
    document.cookie="MYBIGCOOKIE=1";
    

    Your cookies would look like:

    "MYBIGCOOKIE=1; PHPSESSID=d76f00dvgrtea8f917f50db8c31cce9"
    

    first of all read all cookies:

    var read_cookies = document.cookie;
    

    then split all cookies with ";":

    var split_read_cookie = read_cookies.split(";");
    

    then use for loop to read each value. Into loop each value split again with "=":

    for (i=0;i<split_read_cookie.length;i++){
        var value=split_read_cookie[i];
        value=value.split("=");
        if(value[0]=="MYBIGCOOKIE" && value[1]=="1"){
            alert('it is 1');
        }
    }
    
    0 讨论(0)
提交回复
热议问题