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

  • Home
  • SEARCH
  • 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 4066022
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T16:05:07+00:00 2026-05-20T16:05:07+00:00

First off, let me preface this question by saying that my professor is firmly

  • 0

First off, let me preface this question by saying that my professor is firmly entrenched in the past. Our last assignment required us to float links on top of pictures.

You might also say that he’s insane as in order to test our pages he requires that all functionality (including cookies) be implemented with “client side technology” i.e. not on the server. He uses Firefox to test the pages, so the single blessing is that he doesn’t care about cross-browser compatibility.

That being said, I’m having a problem with our latest assignment. We’re making a “shopping cart” system using Javascript and cookies to store the items to be purchased. This is fine, except for some reason in my function that adds a new element to the cookie, assigning something to document.cookie doesn’t work.

You can find my entire site here .zip file download (if there’s anything that you wonder, “why on earth would you do that? That’s crazy!” – that’s either a direct assignment or a way to try and minimize the pain.)

This is my code in question that should be modifying the cookie:

var mycookies = new function (){
    var cookies = document.cookie.split(';');
    var cookie, values;
    this.items = [];
    for(var x = 0; x < cookies.length; x++){
        if(cookies[x] != ""){
            cookie = cookies[x].split('=')[0].trim()
            values = cookies[x].split('=')[1]
            values = values.split(',');
            if(!this.items[cookie]){
                this.items.push(cookie);
                this[cookie] = new function(){};
            }
            this[cookie].size = values[0];
            this[cookie].qty = parseInt(values[1]);
        }
    }
    this.render = function(){
        var values, cookies = "", cookie;
        for(var x = 0; x < this.items.length; x++){
            cookie = this.items[x];
            values = [this[cookie].size, this[cookie].qty].join(',');
            cookies += cookie + "=" + values + '; ';
        }
        return cookies;    
    }                      
    this.clear = function(){
        for(var x = 0; x < this.items.length; x++){
            delete this[this.items[x]];
        }                  
        this.items = [];
        document.cookie['expires'] = '26 Aug 1984 01:01:01 UTC;';
    }                      
    this.additem = function(){
        var i = document.forms[0].size.selectedIndex;
        if (this.items[page]){
            this[page].size = document.getElementById('size').value;
            this[page].qty = document.getElementById('qty').value;
        }                  
        else{              
            this.items.push(page);
            this[page] = new function(){};
            this[page].size = document.getElementById('size').value;
            this[page].qty = document.getElementById('qty').value;
        }
        console.log(this.render()); // For use with firebug
        document.cookie = this.render();
        console.log(document.cookie); // For use with firebug
    }
}

When I fire this off, firebug provides this output:

expires=12 Aug 2001 01:01:01 UTC,NaN; whitec=Small,3;
expires=12 Aug 2001 01:01:01 UTC,NaN

Now, I would expect 1) my cookie to have expired (I set the expiration manually through firebug, my parsing added the NaN later, – yet there it stays), and 2) the value for the cookie to be changed to the result of this.render()

Other than the obvious fact that client-side cookie behavior is not guaranteed by the w3 spec, am I missing something here? (EDIT – what I mean is when the page is client-side, opened as a file – not served by a server) This is really aggravating – I’ve tried a multitude of different angles, and no “javascript cookie” search or “modify cookies javascript” leads me to anything useful. Any suggestions about how I can fix it?

Or should I just email my professor with a link to the w3 specs and tell him that requiring us to support cookies client side is stupid?

  • 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-20T16:05:08+00:00Added an answer on May 20, 2026 at 4:05 pm

    The workings of document.cookie are not what you apparently think they are. When you set a value into the variable, you set one cookie at a time. Thus, if you wanted to set all the cookies you’re holding in your object, you’d loop through your “items” array and set document.cookie successively to each name/value pair (transformed into a “cookieName=cookieValue” string).

    This is a fact in all modern browsers. See this Mozilla documentation page for example.

    Other comments on the code, since you were nice enough to post it:

            cookie = cookies[x].split('=')[0].trim()
            values = cookies[x].split('=')[1]
    

    Better to call “split” just once.

                this[cookie] = new function(){};
    

    That’s essentially equivalent to this[cookie] = {}; to set the property to a new empty object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First off, let me preface this question by stating that I'm really a pretty
First off, let me start by saying, I know this exact question has been
First off, let me start by saying that I am totally new to working
Preface Let me start off by saying that I'm a relatively new programmer and
Ok, let me first start off by saying that I've only ever dealt with
(PHP/MySQL) Let me preface by saying that this works, and that I'm wondering if
First off, let me say that this is not homework (I am an A-Level
Let me preface my question by saying that I am very new to mysql
First off, let's start by saying I'm a rookie. That being said, I am
First off, let me explain that I am well aware of what this exception

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.