Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 03-15-2010, 05:21 PM   PM User | #1
newmngt
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
newmngt is an unknown quantity at this point
[HELP] Whacky JS array access problems!

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.

You can see the effect here. It's the secondary nav under Home, Featured Items, etc. Each token that is clickable is separated by an asterisk:
http://www.mchonejewelry.com/test/newsite/community.php

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!
newmngt is offline   Reply With Quote
Old 03-15-2010, 06:46 PM   PM User | #2
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by newmngt View Post
I have a multidimensional associative array in JS
there’s no such thing in JavaScript. JS Arrays are always numerical. "associative arrays" are called objects*.

* - the difference lies in the built-in methods these objects can use.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 03-15-2010, 06:53 PM   PM User | #3
newmngt
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
newmngt is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
there’s no such thing in JavaScript. JS Arrays are always numerical. "associative arrays" are called objects*.

* - the difference lies in the built-in methods these objects can use.
OK, so you're saying I have created associative arrays that are objects.

I know I can access the array and that I'm creating the array correctly. You can see on the site I've linked to that it works on other pages.

So I don't quite get what you're saying... Are you telling me I'm doing something wrong in the way the array is being accessed?
newmngt is offline   Reply With Quote
Old 03-15-2010, 07:00 PM   PM User | #4
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by newmngt View Post
OK, so you're saying I have created associative arrays that are objects.
nope, I’m saying that you have created Objects, that are not Arrays.

Quote:
Originally Posted by newmngt View Post
I know I can access the array and that I'm creating the array correctly. You can see on the site I've linked to that it works on other pages.
both can be accessed in the same way, the difference is the following:
PHP Code:
var = [2,1,3]; // array
alert(a[1]);
a.sort();
alert(a[1]);

var 
= {a:1,b:2,c:3}; // "associative array"
alert(b["b"]);
b.sort(); // <-- throws an error 
Quote:
Originally Posted by newmngt View Post
So I don't quite get what you're saying... Are you telling me I'm doing something wrong in the way the array is being accessed?
no. just to be careful when talking about arrays, when they’re not. this may be the difference between a working script and a failure.
__________________
please post your code wrapped in [CODE] [/CODE] tags
Dormilich is offline   Reply With Quote
Old 03-15-2010, 07:09 PM   PM User | #5
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
your problem is the following:
line #30
Code:
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
Dormilich is offline   Reply With Quote
Users who have thanked Dormilich for this post:
newmngt (03-15-2010)
Old 03-15-2010, 07:12 PM   PM User | #6
newmngt
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
newmngt is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
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?
newmngt is offline   Reply With Quote
Old 03-15-2010, 07:15 PM   PM User | #7
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
Quote:
Originally Posted by newmngt View Post
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 View Post
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
Dormilich is offline   Reply With Quote
Old 03-15-2010, 07:18 PM   PM User | #8
newmngt
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
newmngt is an unknown quantity at this point
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.
newmngt is offline   Reply With Quote
Old 03-15-2010, 08:24 PM   PM User | #9
newmngt
New to the CF scene

 
Join Date: Mar 2010
Posts: 5
Thanks: 1
Thanked 0 Times in 0 Posts
newmngt is an unknown quantity at this point
Quote:
Originally Posted by Dormilich View Post
filtering off functions can be done this way
Code:
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.
newmngt is offline   Reply With Quote
Old 03-15-2010, 08:45 PM   PM User | #10
Dormilich
Senior Coder

 
Dormilich's Avatar
 
Join Date: Jan 2010
Location: Behind the Wall
Posts: 2,907
Thanks: 10
Thanked 293 Times in 289 Posts
Dormilich is on a distinguished road
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..
Dormilich is offline   Reply With Quote
Reply

Bookmarks

Tags
access, arrays, code leak, javascript, problem

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:58 PM.


Advertisement
Log in to turn off these ads.