Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6626857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:56:38+00:00 2026-05-25T21:56:38+00:00

The following Javascript function returns a JS object: function getCookies() { var result =

  • 0

The following Javascript function returns a JS object:

function getCookies() {
    var result = {};
    var cookie = {};
    var cookies = document.cookie.split(';');
    for (var i = 0; i < cookies.length; i++) {
        cookie = cookies[i].split('=');
        result[cookie[0]] = cookie[1];
    }
    return result;
}

When I tried to access its fields the “easy” way, all I got was “undefined”, eg:

var c = getCookies();
alert(c.a_cookie_name);
alert(c['a_cookie_name']);

The only way I could access the keys and values was iterating through the fields, eg:

for(cookieName in c){
  alert(c[cookieName]);
}

The question is how to access the fields without iterating?

Thank you.

P.S. The keys and values do exist, I can see the object fields with console.log(getCookies()) in Chrome.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T21:56:38+00:00Added an answer on May 25, 2026 at 9:56 pm

    You are properly accessing fields the problem is that hte fields you’re accessing don’t exist. It’ looks like the property named a_cookie_name simply doesn’t exist on the object.

    EDIT

    Given that the Chrome console shows the properties as existing, one possibility to consider is there is white space in the names of the properties. This could explain the difference as the white space would be hard to see in the console. To test that out try the following. It will make the spaces a bit more visible if they are there

    for (var cookieName in c) {
      alert('"' + cookieName + '"="' + c[cookieName] + '"');
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

For the following JavaScript: function EscapedString(str) { var obj = document.createElement(span); obj.innerHTML = str;
I use the following javascript function, function get_check_value(formId,checkboxId,TxtboxId) { alert(formId); var c_value = ;
Given the following javascript (jquery) $(#username).keyup(function () { selected.username = $(#username).val(); var url =
I am trying to do the following in JavaScript: var gete = document.getElementById; But
I found following in javascript code function calculateURI(uri) { return (namespace.push.configuration.uriPrefix || '') +
I have the following JavaScript (I'm using jQuery): function language(language) { var text =
I have the following javascript function person() { //private Variable var fName = null;
I have a JavaScript object that looks like the following: venue = function(map, dataSet)
My service returns the following JSON object, with the Content-Type header set to application/javascript.
Supposing I have the following code which returns a Javascript object which I can

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.