How to capture data from cookie?

Discussion in 'JavaScript' started by dariusd77, Aug 24, 2016.

  1. #1
    Need a javascript that will capture cookie from _document.cookie and then pass it to a variable.
    Also need to strip away some data from cookie. . I only need the portion that says ""AMCVS_C7C319A0574F094D7F000101%40AdobeOrg=1"pass this value to a variable called eVar.. Example of cookie is at the bottom.

    Im thinking script should be something like this.

    function getCookie(cname) {
       var name = cname + "=";
       var ca = document.cookie.split(';');
       for(var i = 0; i < ca.length; i++) {
           var c = ca;
           while (c.charAt(0) == ' ') {
               c = c.substring(1);
           }
           I'm thinking somewhere in here is where I would strip away the unwanted portion.
           }
       }
       return "";
    }
    Code (markup):

    Please help not sure how to go about this.. Thanks.

    Here is an example of a cookie.

    "AMCVS_C7C319A0574F094D7F000101%40AdobeOrg=1; AMCV_C7C319A0574F094D7F000101%40AdobeOrg=-1248264605%7CMCIDTS%7C17037%7CMCMID%7C56086522608320454022024251645676171677%7CMCAAMLH-1472181159%7C7%7CMCAAMB-1472583043%7CNRX38WO0n5BH8Th-nqAG_A%7CMCOPTOUT-1471985443s%7CNONE%7CMCAID%7C2BD5B8EF85078B17-60000106C03A19F8;
    Code (markup):
     
    dariusd77, Aug 24, 2016 IP
  2. PoPSiCLe

    PoPSiCLe Illustrious Member

    Messages:
    4,623
    Likes Received:
    725
    Best Answers:
    152
    Trophy Points:
    470
    #2
    Is the part you need always at the beginning of the cookie? If so all you need to do after the split is a simple ca[0] (with a trim if you don't want the spaces)
     
    PoPSiCLe, Aug 25, 2016 IP
  3. Blizzardofozz

    Blizzardofozz Well-Known Member

    Messages:
    132
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    118
    #3
    If I understood correctly what you need:

    
    function getCookie(cName) {
        var cookieArray = document.cookie.split(";");
       
        for (var i = 0; i < cookieArray.length; i++) {
            if (cookieArray[i].indexOf(cName) !== -1) {
                return cookieArray[i].substring(cookieArray[i].indexOf("=") + 1);
            }
        }
     
        return;
    }
    
    var   cName = "AMCVS_C7C319A0574F094D7F000101%40AdobeOrg";
    
    alert(getCookie(cName));
    
    Code (markup):
     
    Blizzardofozz, Aug 29, 2016 IP