3

Unfortunately I can't easily paste the whole script that generates the variable, but I don't see it would be relevant either. Please instruct for more details, if necessary.

Javascript shows this:

console.log(gl.boxes);

shows:

[{element:{0:{jQuery19104057279333682955:9}, context:{jQuery19104057279333682955:9}, length:1}, boxName:"testi1", boxX:1, boxY:"180"}]

so gl.boxes[0] should exist, right? Still...

console.log(gl.boxes[0])

shows: undefined.

So what can I be missing here?

EDIT: I will paste some more code about the generation of gl.boxes. Should be mostly about creating the variable as array first:

gl.boxes = [];

Then there is a function that handles creating and pushing new objects:

this.addBox = function (box) {
    var retBox = {};
    retBox.element = $(document.createElement('div'));

    retBox.boxName = box.boxName;
    retBox.boxX = box.boxX ? box.boxX : rootParent.defaultX;
    retBox.boxY = box.boxY ? box.boxY : rootParent.defaultY;

    retBox.element
            .html(retBox.boxName)
            .addClass(rootParent.boxClass)
            .offset({ left: retBox.boxX, top: retBox.boxY })
            .draggable({
                stop: gl.URLs.dragStopDiv(retBox)
            });
    retBox.element.appendTo(rootParent.containerDiv);
    gl.boxes.push(retBox);
    return retBox;
};

The objects are created based on URL. Ie. in this test I have inline JS:

gl.objects.addBox({"boxName":"testi1","boxX":"50","boxY":"180"});

Only other place where the gl.boxes is being used is generating a URL based on the objects:

for(key in gl.boxes) {
    var position = gl.boxes[key].element.position();

    uri += 
        "boxName["+key+"]="+gl.boxes[key].boxName+"&"+
        "boxX["+key+"]="+position.left+"&"+
        "boxY["+key+"]="+position.top+"&";
}
Hachi
  • 536
  • 6
  • 17
  • what is `console.log(typeof gl.boxes);` ? I ask because it could be a string. – obecker Jul 30 '13 at 12:28
  • Are you deleting elements from the array before this point, using `delete` - see http://stackoverflow.com/questions/500606/javascript-array-delete-elements? – Adrian Wragg Jul 30 '13 at 12:28
  • @Adrian: There should be no deletion at any point. Basically both of the console.log-commands are straight after one another and the boxes is a collection of objects and should not be deleted at any point. – Hachi Jul 30 '13 at 12:31
  • @obecker: Shows Object – Hachi Jul 30 '13 at 12:31
  • Try `console.dir(gl.boxes)`, sometimes it makes it a little more clear then. – seymar Jul 30 '13 at 12:32
  • use `JSON.stringify(gl.boxes[0])` – HIRA THAKUR Jul 30 '13 at 12:34
  • @MESSIAH I assume you mean I will ie. console.log it: "console.log(JSON.stringify(gl.boxes[0]));"... shows undefined. – Hachi Jul 30 '13 at 12:44
  • 7
    `console.log(gl.boxes.length); console.log(typeof gl.boxes[0]); console.log(Object.prototype.toString.call(gl.boxes));` – basilikum Jul 30 '13 at 12:49
  • 1
    I guess,post some code which will indicate how this gl.boxes was created!!!! – HIRA THAKUR Jul 30 '13 at 12:50
  • @basilikum the output are these: "0", "undefined", "[object Array]" – Hachi Jul 30 '13 at 12:57
  • what about `JSON.stringify (gl.boxes)` – Moritz Roessler Jul 30 '13 at 12:59
  • @MESSIAH I will... but also part of the problem I wish to solve is to know in what status is the gl.boxes, since understanding that can help me solve these issues easier in the future. At the present I can not comprehend why it gives such seemingly illogical output. – Hachi Jul 30 '13 at 13:01
  • 2
    as you wish sir...if people will see the code they will come up with a answer + the reason.. – HIRA THAKUR Jul 30 '13 at 13:02
  • @MESSIAH it's not that I just avoid putting the code there (though a bit :). It's more that I assumed there some javascript behaviour I was not seeing originally. The code has been edited to the post. I hope it's sufficient? – Hachi Jul 30 '13 at 13:10
  • 1
    @Hachi in this case you are dealing with an empty array for whatever reason. Your code doesn't look too bad as far as I can tell. The only thing that I noticed, is that you use a `for..in` loop to iterate over the items in your array. That probably isn't responsible for the empty array, but normally you would use `for (var i = 0; i < arr.length; i++)` for array iteration. But another thing that would be nice to know, is where do you put the two lines `console.log(gl.boxes);` and `console.log(gl.boxes[0]);` in your code? Did they follow directly of one another? – basilikum Jul 30 '13 at 13:17
  • @basilikum ah, that's it! The console.logs are actually before the inline javascript (gl.objects.addBox)... Though I don't know where those non-existing values come from though, can the values transfer from the old page-load to the new one, as sort of ghost-values that point to something non-existant? I use php to manage the URLs, since it seemed to be a lot easier to translate the URLs to javascript objects via PHP than javascript. That's why the inline is messing up the javascript. – Hachi Jul 30 '13 at 13:43
  • possible duplicate of [Is Chrome's JavaScript console lazy about evaluating arrays?](http://stackoverflow.com/questions/4057440/is-chromes-javascript-console-lazy-about-evaluating-arrays) – Felix Kling Aug 04 '13 at 09:50

1 Answers1

0

Maybe you need to change your loop to use indexes:

var i = 0,
    position = {};

for (i = 0; i < gl.box.length; i += 1) {
    position = gl.boxes[i].element.position();
    uri += "boxName[" + i + "]=" + gl.boxes[i].boxName + "&" + "boxX[" + i + "]=" + position.left + "&" + "boxY[" + i + "]=" + position.top + "&";
}
Kim T
  • 5,770
  • 1
  • 52
  • 79