...

View Full Version : Calling a function from a parameter of another function



weichenxi
06-29-2009, 07:13 AM
Hello...

Forgive me if this has been asked a thousand times already...

I have a javascript image viewer which I managed to get working just the way I want, but after three days of non-stop work, I'm finally asking for help on the one part that I snagged myself on.

Captions for the images are called from the parameters of the main function and delivered into a span or div tag with the appropriate id. A very important part of what needs to be in the captions is text compiled by a separate function, in the project library.

I'm sort of new to js, so please bear with me if I've gotten some of the terminology wrong.

I've tried this with and without curly, square and round brackets, with and without semicolons and quotes- all with varying results. Sometimes the function is called, but it appears at the very top of the page and nowhere near the proper <span> tag.

Here is the code:


addImage(a,b,c,d);
// filename, image name and caption are in a, b and c.
// d is the variable where I need to write my other function...
addRef(t,s);
// I need to put addRef in the d parameter of addImage.

Any help would be greatly appreciated!! I've lost my entire weekend and about 14 hours of sleep on this one.

Thanks in advance.

Kind regards,
Rich Wertz

Old Pedant
06-29-2009, 07:32 AM
Not enough code to understand what it is you are trying to do.

My first instinct was to say you would do

addImage( a, b, c, addRef( t, s ) );

And that may, indeed, be correct. But it's hard to believe you didn't already try that, so I'm guessing that there is some side effect to WHEN the function is called that we can't see from your too-brief code drop.

weichenxi
06-29-2009, 10:10 AM
Sorry... I don't ever know how much code is needed...

The function that is used to add each image is below. The function is placed in a separate js file- one instance per image- with filename, caption, link name and hopefully reference, in the parameters (again- no idea if this is the correct terminology).


function addImg(nmeFi, nmeLi, cap, ref) {
var len = images.length;
img[len] = nmeFi;
caps[len] = cap;
refs[len] = ref;
if (typeof nmeLi == "undefined") {
nmeLis[len] = len + 1;
} else {
nmeLis[len] = nmeLi;
}
if (typeof nmeFi == "undefined") {
navNms[len] = len + 1;
} else {
navNms[len] = nmeFi;
}
if (typeof cap == "undefined") {
caps[len] = len + 1;
} else {
caps[len] = cap;
}
if (typeof ref == "undefined") {
refs[len] = len + 1;
} else {
refs[len] = ref;
}
}

The separate js file would contain a list of this function with different parameters for each image:



addImage("kermit.jpg","01","<p>CEO</p><p>Dr. Kermit</p>",addRef(5,"Ker"));
addImage("han.jpg","01","<p>CFO</p><p>Dr. Solo</p>",addRef(3,"Ker"));
addImage("phil.jpg","01","<p>COO</p><p>Dr. Fry</p>",addRef(4,"Tur"));
addImage("caesar.jpg","01","<p>Chairman</p><p>Julian</p>",addRef(1,"Jul"));


The second function is this:


function addRef(t,s) {

var nd="Biography</a></li>"

document.write([hr()]+'<p class="bio">SEE Also:<ul class="bio">
<li><a target="_blank" href="/biodraw/board/curr/bio.')

if(t == 1) {document.write('1na.html#'+((s))+'">'+nd)} //north america
if(t == 2) {document.write('2eu.html#'+((s))+'">'+nd)} //europe
if(t == 3) {document.write('3as.html#'+((s))+'">'+nd)} //asia
if(t == 4) {document.write('4sa.html#'+((s))+'">'+nd)} //south america
if(t == 5) {document.write('5au.html#'+((s))+'">'+nd)} //australia

document.write('</ul></p>')
}

When I try any manner of ways to call addRef in addImg as a parameter, either the string of the function is written, or the function produces what I want, but fails to put it where I want it- which is inside of a <span> tag and instead, places it at the very top of the page. If left without a value, the parameter defaults to the image number and when it does, that is in the correct spot, within the correct tag.

Please let me know if I should clarify anything... I'm not a coder by trade, only by necessity, so it's sort of intimidating to ask for help from experts... especially when I don't know all of the terminology.

Apologies if I'm making this hard to answer!

Thanks again for the help!

Kind regards,
Rich

Old Pedant
06-29-2009, 10:52 PM
Hmmm...so your call to addImage() is just putting values into arrays for LATER use in the page, right?

So the big question: Do you *WANT* to see *ALL* of the addRef() outputs on the page, or just the one (or ones) that are associated with one of those images that you later use??

See, the problem is that your addRef() function is indeed *IMMEDIATELY* writing stuff to the browser screen, when it is called. All the other values are just squirreled away for later use. If you wanted the stuff the addRef() is producing to *also* be squirreled away, then it would be easy to rewrite addRef() to do that.

But if you need all 4 of those addRef() outputs to show (4 calls to addImage() and thus to addRef() in the example code you gave), then you will need to somehow add them into a particular section of the screen and, presumably, remember where they are as the return value from addRef( ).

If you could show this page "live" someplace (even if with dummy images?) and show us what it is doing now and tell us what it needs to do, that would help.

Acting on the HUNCH that you don't really want to output the stuff from addRef() until an image is pulled out of the arrays, I would *guess* that you would rewrite it thus:


addRefInfo = ["0ERROR","1na","2eu","3as","4sa","5au"];

function addRef(t,s)
{
return
hr()
+ '<p class="bio">SEE also:<ul class="bio">'
+ '<li><a target="_blank" '
+ ' href="/biodraw/board/curr/bio.' + addRefInfo[t] + '.html#' + (s) + '">'
+ 'Biography</a></li>'
+ '</ul></p>';
}

weichenxi
06-30-2009, 05:41 AM
Hmmm...so your call to addImage() is just putting values into arrays for LATER use in the page, right?

So the big question: Do you *WANT* to see *ALL* of the addRef() outputs on the page, or just the one (or ones) that are associated with one of those images that you later use??

See, the problem is that your addRef() function is indeed *IMMEDIATELY* writing stuff to the browser screen, when it is called. All the other values are just squirreled away for later use. If you wanted the stuff the addRef() is producing to *also* be squirreled away, then it would be easy to rewrite addRef() to do that.

But if you need all 4 of those addRef() outputs to show (4 calls to addImage() and thus to addRef() in the example code you gave), then you will need to somehow add them into a particular section of the screen and, presumably, remember where they are as the return value from addRef( ).

If you could show this page "live" someplace (even if with dummy images?) and show us what it is doing now and tell us what it needs to do, that would help.

Acting on the HUNCH that you don't really want to output the stuff from addRef() until an image is pulled out of the arrays, I would *guess* that you would rewrite it thus:


addRefInfo = ["0ERROR","1na","2eu","3as","4sa","5au"];

function addRef(t,s)
{
return
hr()
+ '<p class="bio">SEE also:<ul class="bio">'
+ '<li><a target="_blank" '
+ ' href="/biodraw/board/curr/bio.' + addRefInfo[t] + '.html#' + (s) + '">'
+ 'Biography</a></li>'
+ '</ul></p>';
}


Wow!

So first of all, thank you for your help on this. I can't do anything on my project just yet since the servers were down Monday for maintenance and will be again tomorrow (Tuesday).

You are correct in all of your assumptions and hunches- I do only want to see the output from the associated image that is clicked on or navigated to.

Ok, so I'm going to try this and see if it works as soon as I can and post about the results. As far as a live dummy version... I have to retrieve the files, since this is on an intranet and not available from outside... I could have one working online inside of a day or two.

In the meantime, you've given me alot to absorb... I can't thank you enough for your help! I'll let you know how it all works out!

Thanks again!

-Rich

weichenxi
07-01-2009, 01:21 AM
Hello...

I stayed late just to see if this was going to work... and IT DID!!!!!

Thank you so much!!!

Not only did your solution solve my immediate problem, but it also solved another problem that someone else was having.

Many, many thanks!

If you're ever in Pittsburgh, I'll buy you a coffee!:thumbsup:

Old Pedant
07-01-2009, 07:48 PM
Oh, I already served my time in Pittsburgh. Two years at Carnegie Tech (yeah, before it became Carnegie-Mellon). Two of my grandparents were born there, too. <grin/>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum