Javascript Issues

First of all, let me pose this question by saying that my professor is firmly rooted in the past. Our last assignment required us to float links over images.

You can also say that it is crazy, because to check our pages it requires all the functionality (including cookies) to be implemented using "client technology", that is, not on the server. It uses Firefox to check pages, so the only blessing is that it does not care about browser compatibility.

Having said that, I had a problem with our last assignment. We are creating a “shopping basket” system using Javascript and cookies to store items that need to be purchased. This is fine, except for some reasons of my function, which adds a new element to the cookie, assigning something to document.cookie does not work.

You can find my entire site here .zip to download the file (if there is everything that you are wondering: “Why would you do this? This is crazy!” Is either a direct task or a way to try to minimize pain.)

This is my code that should change 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 run this, 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

, 1) cookie ( firebug, NaN , - ) 2) cookie, this.render()

, cookie w3, - ? ( - , , - ) - , "javascript cookie" " cookie javascript" - . , ?

w3 , , cookie, ?

+3
3

document.cookie , , -, . , cookie . , cookie, , "items" document.cookie / ( "cookieName = cookieValue" ).

. . Mozilla.

, , :

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

"split" .

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

this[cookie] = {};, .

+3

, - ? cookie.

0
0

Source: https://habr.com/ru/post/1796531/


All Articles