I have a multidimensional associative array in JS which contains category information for a navigation. It's actually set up through PHP, which pulls the info from a database then translates it into JS. I know it's set up correctly since I can see it in the source once the page is rendered.
However, accessing this array causes external JS code (from my Slimbox files) to be displayed in the nav. It's as if the array is set up right, but accessing the elements brings in Slimbox code as the elements. It's very strange.
Notice the other page's navs work great! Try the jewelry nav on the Home page and the categories are set up fine. This is because other pages do not link to Slimbox.
(And no, the site aint pretty because I wanted to get the hard stuff working before I work on the look)
Any clue what's going on? I have no idea how this can be happening. I've heard of jQuery interfering with Slimbox and Lightbox, but my nav is not using jQuery!
for(var menu2Cur in existingCats[menu1Selection]){
existingCats[menu1Selection] is an Object (not an Array!) and the for-in loop cycles through all object properties, that are not built in, even if they are defined elsewhere. that includes also functions that are associated with that object.
filtering off functions can be done this way
PHP Code:
for (prop in obj) { if (obj.hasOwnProperty(prop) && !(obj[prop] instanceof Function)) { // do something with obj[prop] } }
__________________
please post your code wrapped in [CODE] [/CODE] tags
nope, I’m saying that you have created Objects, that are not Arrays.
[...]
just to be careful when talking about arrays, when they’re not. this may be the difference between a working script and a failure.
OK, I mistyped. I am not creating associative arrays because they are not supported by javascript. Am I correct in understanding that I'm creating objects and defining properties on the fly?
I suppose if that's the case, I'll need to think about the data structure as no longer being an array, but instead objects with properties. This still does not explain why external code is being pulled in as values, since if I am creating properties of objects, why wouldn't the code just crash?
OK, I mistyped. I am not creating associative arrays because they are not supported by javascript. Am I correct in understanding that I'm creating objects and defining properties on the fly?
correct. JavaScript is extremely flexible
Quote:
Originally Posted by newmngt
I suppose if that's the case, I'll need to think about the data structure as no longer being an array, but instead objects with properties. This still does not explain why external code is being pulled in as values, since if I am creating properties of objects, why wouldn't the code just crash?
see post above.
__________________
please post your code wrapped in [CODE] [/CODE] tags
I'm typing in replies as you're replying so I'm one step behind you!
I think I understand now. The for loop is looping over all fields in the Object, since it is not an array. Thus built-in fields that are not defined by me may be pulled in.
Still, this does not explain how the code is working on other pages... I would think that the code would crash there too since the for loops are not correctly accessing what I think is an array.
Regardless, this is a huge part of the problem and I really appreciate you pointing this out.
for (prop in obj) {
if (obj.hasOwnProperty(prop) && !(obj[prop] instanceof Function)) {
// do something with obj[prop]
}
}
I added this check to the code and it works. Really, this is a kludge, since I'm not exactly fixing the underlying problem. To truly solve this problem would involve a different code solution that didn't pretend to use associative arrays.
I wouldn’t call it a kludge. it’s just the way, objects work. there are certainly better ways to implement that, but I suppose that’s too advanced at your current stage. the only way out I can imagine is using Arrays.
maybe you’re interested in the Suckerfish Dropdown Menu?
__________________
please post your code wrapped in [CODE] [/CODE] tags
Last edited by Dormilich; 03-15-2010 at 08:48 PM..