469,950 Members | 1,914 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,950 developers. It's quick & easy.

calling a variable as if it was a function

I wonder if this is possible.. i have a couple of anchors inside a
page and all have 'pretty' URLs in the form of 'foo/bar/' and 'foo/
béar/'. If the user clicks on such an anchor (they are buttons) i
don't want them to navigate there but instead i wish a javascript
function te be executed... not that difficult huh?

however i noticed that each of my urls is guarenteed to be unique and
i wish to have for the url '/foo/bar/' an action called foobarAction.
For '/foo/bear' i want it to be foobearAction.. but lazy as i am i
don't want to write a seperate onclick handler for each element.

So what i basically wish to accomplish is to read the 'href' part (/
foo/bear/) get rid of the slashes (as i am in FF 2.0 it's just /foo/
bear/ btw i am aware that in other browsers this could become
http://mysite.com/foo/bear) and call it as were it a function..

The part to read the contents of the href is easy, getting rid of the
slashes and concatenating it to a string is also easy. I setup a nice
function with exactly the same name (yep.. checked 3 times i didnt
make typos) and then called the function.. and it says it doesnt
exist :(

I tried this:

function foobarAction () {alert ('hello world!');}

method = <content of href of <a href="/foo/bar">click me</a+
'Action';
method();

and

foobarAction = function foo () {alert ('hello world!');
method = <content of href of <a href="/foo/bar">click me</a+
'Action';
method();

both times no 'hello world' :(

I am now thinking such a cast from a variable to a function or
'calling a variable' in this paticular way cannot be done. Am i
right??

Thanks in advance

Feb 21 '07 #1
2 1071
k.**********@gmail.com said the following on 2/21/2007 4:18 PM:

<snip>
I am now thinking such a cast from a variable to a function or
'calling a variable' in this paticular way cannot be done. Am i
right??
No, you are not right.

functionToCall = <content of href of...+ "Action";
window[functionToCall]();

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Feb 21 '07 #2
No, you are not right.
>
functionToCall = <content of href of...+ "Action";
window[functionToCall]();

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ -http://jibbering.com/faq/index.html
Javascript Best Practices -http://www.JavascriptToolbox.com/bestpractices/
i stand corrected! it works :) thank you very much!

Feb 22 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by R.G. Vervoort | last post: by
1 post views Thread by Andrew Wilkinson | last post: by
8 posts views Thread by Muthu | last post: by
2 posts views Thread by Joe | last post: by
reply views Thread by Przemys³aw Bana¶ | last post: by
8 posts views Thread by Jeff | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.