Getting a Specific Cookie Value by Key using Jquery cookie

This JavaScript function that gets a cookie value by the key. So if you’re interested in getting a particular value from a key-value pair in the cookie you would just enter the name of the cookie and the name of the key. If the cookie exists and the value is set then it will return the value. Otherwise it will return false.

/*********************************************
* Checks and gets Cookie value by key name
* * Uses: jquery cookie
* * returns false if cookie is null
* * returns cookie value if cookie is set
**********************************************/

function GetCookieValueByKey(cookieName, keyName) {
    var x, xobj, cookieString;
    var cookieStringObject;
    var cookieVal = "";

    cookieString = $.cookie(cookieName);
    if (cookieString == null || cookieString == '') return false;

    cookieStringObject = cookieString.split("&");
    for (x in cookieStringObject){
        xobj = cookieStringObject[x].split('=');
        if (xobj[0] == keyName) cookieVal = xobj[1];
    }

    if (cookieVal == '') return false;
    return cookieVal;
}

Like this post?

Never fall behind again. Keep up with a weekly digest of the
best posts from here and around the web.