homepage Welcome to WebmasterWorld Guest from 54.197.215.146
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
Forum Library, Charter, Moderator: open

JavaScript and AJAX Forum

    
Using parameter value as an array's name
j2trumpet




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

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);
}

 

Dijkgraaf




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

ref = category + ".length";
catlength = eval(ref);

j2trumpet




msg:4055607
 1:07 am on Jan 7, 2010 (gmt 0)

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

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

j2trumpet




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

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

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

Dijkgraaf




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

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.

daveVk




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

A more direct way

ref = window[category].length;

and

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

j2trumpet




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

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>');
}

j2trumpet




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

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!

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / JavaScript and AJAX
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved