How do I capture data from a cookie?

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[i];
       while (c.charAt(0) == ' ') {
           c = c.substring(1);
       }
       // I'm thinking somewhere in here is where I would strip away the unwanted portion.
       }
   }
   return "";
}

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

Here is an example of a cookie.

“AMCVS_C7C319A0574F094D7F000101%40AdobeOrg=1; AMCV_C7C319A0574F094D7F000101%40Adobe
Org=-1248264605%7CMCIDTS%7C17037%7CMCMID%7C56086522608320454022024251645676171677%
7CMCAAMLH-1472181159%7C7%7CMCAAMB-1472583043%7CNRX38WO0n5BH8Th-nqAG_A%7CMCOPT
OUT-1471985443s%7CNONE%7CMCAID%7C2BD5B8EF85078B17-60000106C03A19F8; _sdsat_landing_page=http://atlanticbay.com/|1472063445691; _sdsat_session_count=2; _hjIncludedInSample=1; tagtician_firstload=true; _sdsat_lt_pages_viewed=40; _sdsat_pages_viewed=2; _sdsat_traffic_source=; mbox=check#true#1472071456|session#1472071395938-502888#1472073256; s_cc=true; AMCV_C7C319A0574F094D7F000101%40AdobeOrg=-1248264605%7CMCIDTS%7C17037%7CMCMID%
7C56086522608320454022024251645676171677%7CMCAAMLH-1472181159%7C7%7CMCAAMB-1472676196%7CNRX38WO0n5BH8Th-nqAG_A%7CMCOPTOUT-1472078596s%7CNONE%7CMCAID%7C2BD5B8EF85078B17-60000106C03A19F8; _ga=GA1.2.130388394.1470839896; _gat=1; s_sq=abmglive-staging%3D%2526c.%2526a.%2526activitymap.%2526page%253Dhttp%25253A%25252F%25252F
atlanticbay.com%25252F%2526link%253DTag%252520Found%2526region%253DjsConsole%2526.
activitymap%2526.a%2526.c%2526pid%253Dhttp%25253A%25252F%25252Fatlanticbay.com%2525
2F%2526oid%253Dfunctiononclick%252528event%252529%25257Bdocument.getElementById%25252
8%252527bodyCode%252527%252529.style.display%25253D%252527inline%252527%25253Beval%25
2528document.getE%2526oidt%253D2%2526ot%253DBUTTON%26abmglive%252Cabmgblog1%252Cab
mgabcares%3D%2526c.%2526a.%2526activitymap.%2526page%253Dhttp%25253A%25252F%25252F
stories.atlanticbay.com%25252Findex%25252F%2526link%253DTag%252520Found%2526region%253
DjsConsole%2526.activitymap%2526.a%2526.c%2526pid%253Dhttp%25253A%25252F%25252Fstories.
atlanticbay.com%25252Findex%25252F%2526oid%253Dfunctiononclick%252528event%252529%25257B
document.getElementById%252528%252527bodyCode%252527%252529.style.display%25253D%252527
inline%252527%25253Beval%252528document.getE%2526oidt%253D2%2526ot%253DBUTTON”

Separate the functionality to get the cookie and to extract the data you need.

// Get the cookie

function getCookie(name) {
  var value = "; " + document.cookie;
  var parts = value.split("; " + name + "=");
  if (parts.length == 2) return parts.pop().split(";").shift();
}

var myCookie = getCookie(cookieName);

Assuming that myCookie contains the value from your post above, you can now just split at semi-colons and take the first element of the returned array:

var data = myCookie.split(";")[0];
console.log(data);
// AMCVS_C7C319A0574F094D7F000101%40AdobeOrg=1
1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.