aspose file tools*
The moose likes HTML, CSS and JavaScript and the fly likes Dynamic Javascript name Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Dynamic Javascript name" Watch "Dynamic Javascript name" New topic
Author

Dynamic Javascript name

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
I will only know the names of Javascript functions at runtime. Is there a way to do it? Example:

function callit(num) {
// if num = 1 I want call function myfunction_1
// if num = 2 I want call function myfunction_2
// ...
// if num = n I want call function myfunction_n


}

How to do it?

Thanks!


BJ - SCJP and SCWCD
We love Java programming. It is contagious, very cool, and lot of fun. - Peter Coad, Java Design

Crazy Bikes created by m-Power
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61239
    
  66

Just looks like a series of ifs to me.

What do you mean by "at runtime"? You're not explaining this very well.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks Bear.

I want make a function name at runtime:

var func = "myFunction" + num;
func();

This does not work.

This will work : (but I need 1000 to be attached at runtime)

var func = myFunction1000;
func();

function myFunction999() {}
function myFunction1000() {}
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61239
    
  66

Are the functions already defined? If so, calling a function whose name is in a string variable is easy:

Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks bear.
It works!
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61239
    
  66

Make sure you understand why it works. If not, ask...
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What do the functions do? I find it unlikely this can't be done more cleanly, but without the functionality being defined it's hard to say.
Bruce Jin
Ranch Hand

Joined: Sep 20, 2001
Posts: 666
Thanks bear.

I figure your window[] contains references to all functions and other objects as well.
I was wondering where to find materials to study this a little more. I have used Javascript a few years and have several Javascript books but I did not know this window[] thing.


Thanks David. yes it is a little weired the way I was coding like that. But sometimes...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Dynamic Javascript name