Page 1 of 1

Dynamic Functions

Posted: Sun Jul 27, 2003 8:51 pm
by Gen-ik
This is probably a question for the more 'advanced' users of JavaScript.
How would I go about adding new lines of code to a JavaScript function?

Let's say I had this function...

function alertnames()
{
alert('Bob');
alert('Charlie');
}


... and I wanted to add ... alert('David') ... to that function.

I'm not worried about it working on any browser apart from IE5+ so if it doesn't work on NS no problem.

I'm sure the new Microsoft DOM for IE5+ allows you to do this and I've been going through the Microsoft website but haven't found anything yet.


Any help on this would be really cool.

Thanks.

Posted: Mon Jul 28, 2003 5:26 am
by Fredix
Sorry that I can't give no answer to the question: how to edit a JS file but I noticed something diffrent:
you normally would use

Code: Select all

function names(name)
{
 alert(name)
}
to give the alert message
and have something like this in the HTML code:

Code: Select all

<input type="button" name="noname" value"my name is..." onclick="names(Bob)" />
<input type="button" name="noname" value"my name is..." onclick="names(Charlie)" />
<input type="button" name="noname" value"my name is..." onclick="names(David)" />

Posted: Mon Jul 28, 2003 5:29 am
by Fredix
oh just a thought:
If all the names are in eg. a database you can generate the JS code with PHP and let it alert all know names.......

Posted: Mon Jul 28, 2003 10:53 am
by Gen-ik
Yeah the example I gave above was just something to show you what I was rambing on about :) The actual script I'm working on is a lot more complicated that alert()ing names ;)

I think I have found a way of doing it though, it's not the nicest looking code in the world but it seems to work ok.

Basically what I am doing now is storing lines of code in an array and then push()ing and pop()ing lines of code to the array... and using a stand loop to eval() each line of code.

Here's a quick example.

Code: Select all

var CODE = new Array('Alert("Bob")','Alert(X*13.1)');

function runCODE()
&#123;
for(var i=0; i<CODE.length; i++)
&#123;
eval(CODE&#1111;i]);
&#125;
&#125;

runCODE();
..seems to do the trick. It's obviously not what I was after but we gotta so what we gotta do.