Welcome to WebmasterWorld Guest from 184.73.126.70

Forum Moderators: open

Message Too Old, No Replies

Using parameter value as an array's name

   
10:46 pm on Jan 6, 2010 (gmt 0)

5+ Year Member



I need to be able to use the value of a passed function parameter to find the length of an array with the same name. Everything I've tried just gives me the length of the string value being passed. My goal is to be able to change the parameter and automatically change which array the HTML is accessing. Please help me! Here's a bit of my code:

HTML:
<script type="text/javascript">galleryLink('news');</script>

JavaScript:
var news = new Array();
news[0] = '946,News Highlights';
news[1] = '82394,Weird News Highlights';
news[2] = '948,Top Entertainment';
news[3] = '179347,News Photos';
news[4] = '947,Sports Highlights';

function galleryLink(category) {
do {
var randomGallery = Math.floor(Math.random()*10);
}
while(randomGallery>=category.length);
}

12:57 am on Jan 7, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



ref = category + ".length";
catlength = eval(ref);
1:07 am on Jan 7, 2010 (gmt 0)

5+ Year Member



Thanks! There's one more bit of code I forgot to put originally that I need similar help for:

var gallery = category[randomGallery].split(',');

1:12 am on Jan 7, 2010 (gmt 0)

5+ Year Member



I just tried using this code and it seems to work. Does this look valid to you?

var gallery = eval(category + "[randomGallery].split(',')");

1:28 am on Jan 7, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Yes, that gets you an array of the two items.

However you have a problem in the line
var randomGallery = Math.floor(Math.random()*10);
I think you want to change that * 10 with the catlength, otherwise
1) It will sometimes exceed the size of the array (if it is smaller than 10).
2) Will never get items in the array that are beyond the 10th item.

Then you can get rid of that while loop as well, as I think all you are doing there is discarding ones that are out of range.

1:45 am on Jan 7, 2010 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



A more direct way

ref = window[category].length;

and

var gallery = window[category][randomGallery].split(',');

10:59 pm on Jan 7, 2010 (gmt 0)

5+ Year Member



Thanks guys! My final code looks like this. Let me know if you see any potential problems.

HTML

<script type="text/javascript">p2Galleries('news');</script>

JavaScript

var news = new Array();
news[0] = '946,News Highlights';
news[1] = '82394,Weird News Highlights';
news[2] = '948,Top Entertainment';
news[3] = '179347,News Photos';
news[4] = '947,Sports Highlights';

function p2Galleries(category) {
var catlength = window[category].length;
var randomGallery = Math.floor(Math.random()*catlength);
var gallery = window[category][randomGallery].split(',');
var number = gallery[0];
var title = gallery[1];
document.write('<a href="javascript:open_photo_popup(' + number + ')">' + title + '</a>');
}

11:03 pm on Jan 7, 2010 (gmt 0)

5+ Year Member



FYI - That "javascript:open_photo_popup" is something forced on me by my company's template, so that's something I can't change. Just letting you know so that you don't waste time recommending something else. Thanks again!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month