473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

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 1183
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

10
by: R.G. Vervoort | last post by:
I am using a javafunction (onclick in select) in which i am calling a function in php (thats why i send this to both php and javascript newsgroups). in the onclick i call the function...
1
by: Andrew Wilkinson | last post by:
Hi, First off I know that in almost all cases this would be a terrible thing to do, but this is an unusual case where this makes sense. Basically I have a procedure where you pass a string...
8
by: Muthu | last post by:
I've read calling conventions to be the order(reverse or forward) in which the parameters are being read & understood by compilers. For ex. the following function. int Add(int p1, int p2, int...
2
by: Joe | last post by:
I have 3 functions: ClientInfoA is doing something ClientInfoB is doing something SelectFunction2Run is a function to determine which function needed to run based on the value of the variable...
7
by: Regnab | last post by:
I know this has been covered before but being a beginner at SQL, and only marginally better at VBA, I'm struggling. All I wish to do is call the following function which is in "Microsoft Office...
0
by: Przemys³aw Bana¶ | last post by:
Hello! Can anyone help me with calling Novell function from dll? I think my main problem is in translating C variable types to C# types. Here is the code: using System; using...
4
by: Bugs | last post by:
Hi, I wonder if anyone can help me out. I'm building a vb.net application that has a form with a panel that contains several other sub forms (as a collection of controls). What I'm wanting to...
18
by: John Friedland | last post by:
My problem: I need to call (from C code) an arbitrary C library function, but I don't know until runtime what the function name is, how many parameters are required, and what the parameters are. I...
8
by: Jeff | last post by:
Still new to vb.net in VS2005 web developer... What is the proper/standard way of doing the following - setting the value of a variable in one sub and calling it from another? E.g., as below....
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.