views:

202

answers:

2

I am working on dynamically creating some Javascript that will be inserted into a web page as it's being constructed.

The Javascript will be used to populate a listbox based on the selection in another listbox. When the selection of one listbox is changed it will call a method name based on the selected value of the listbox.

For example:

Listbox1 contains:

Colours
Shapes

If 'Colours' is selected then it will call a "populate_Colours" method that populates another listbox.
To clarify my question: how do I make that "populate_Colours" call in Javascript?

+8  A: 

Assuming the 'populate_Colours' method is in the global namespace, you may use the following code, which exploits both that all object properties may be accessed as though the object were an associative array, and that all global objects are actually properties of the window host object.

var method_name = "Colours";
var method_prefix = "populate_";

// Call function:
window[method_prefix + method_name](arg1, arg2);
Triptych
Thanks for the response. The 'window' bit really threw me until I googled it and found that global objects are part of the window object. Now it makes sense! Thank you.FYI I found a good page about it here http://devlicio.us/blogs/sergio_pereira/archive/2009/02/09/javascript-5-ways-to-call-a-function.aspx
Glad it helped! You should accept this answer if it worked out for you
Triptych
+1  A: 

As Triptych points out, you can call any global scope function by finding it in the host object's contents.

A cleaner method, which pollutes the global namespace much less, is to explicitly put the functions into an array directly like so:

var dyn_functions = [];
dyn_functions['populate_Colours'] = function (arg1, arg2) { 
                // function body
           };
dyn_functions['populate_Shapes'] = function (arg1, arg2) { 
                // function body
           };

This array could also be a property of some object other than the global host object too meaning that you can effectively create your own namespace as many JS libraries such as jQuery do. This is useful for reducing conflicts if/when you include multiple separate utility libraries in the same page, and (other parts of your design permitting) can make it easier to reuse the code in other pages.

David Spillett
That's a nice way of keeping it clean. How would I call the method though? Would window[dyn_functions['populate_Colours'](arg1, arg2) work?
Answering my own question - I've just tested window[dyn_functions['populate_Colours'](arg1,arg2)];and it does indeed work.
you would not need window
epascarello
As epascarello points out, you usually don't need "window" - "dyn_functions['populate_Colours'](arg1,arg2);" will work. In fact not including the global object's name will make code more portable if you are writing routines that might ever be used in a JS environment other than a web browser. There is an exception to this though: if you have a local variable called dyn_functions in a function then you would need to be more specific which you are referring to, but this situation is best avoided (by having sensible naming conventions) anyway.
David Spillett
That's very helpful - thank you