how to add new function?

J

jkarpago

Hi,
I need to add a new function programatically.

I tried this:

var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.id = 'addedscript';
textNode = document.createTextNode("alert('hi');");
oScript.appendChild(textNode);
document.getElementsByTagName('head')[0].appendChild(oScript);

It works in firefox but not in ie.
Can anyone give me a solution?
thanks so much;
 
M

marss

jkarpago said:
Hi,
I need to add a new function programatically.

I tried this:

var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.id = 'addedscript';
textNode = document.createTextNode("alert('hi');");
oScript.appendChild(textNode);
document.getElementsByTagName('head')[0].appendChild(oScript);

I guess you selected too complicated way to resolve the problem.
Addition of new function is equivalently to addition of new variable.
Example:

<html>
<head>
<script type="text/javascript">

var foo = function (){ alert('hi');};

</script>
</head>
<body>
<input type="button" onclick="foo();" value="Say 'hi'">
</body>
</html>

Maybe it helps.
 
R

Randy Webb

jkarpago said the following on 3/17/2006 6:01 AM:
Hi,
I need to add a new function programatically.

I tried this:

var oScript = document.createElement('script');
oScript.type = 'text/javascript';
oScript.id = 'addedscript';
textNode = document.createTextNode("alert('hi');");

oScript.text = "alert('hi')";
oScript.appendChild(textNode);
document.getElementsByTagName('head')[0].appendChild(oScript);

It works in firefox but not in ie.
Can anyone give me a solution?
thanks so much;

Set the script elements .text property.
 
T

Thomas 'PointedEars' Lahn

marss said:
[...]
Addition of new function is equivalently to addition of new variable.

Or a new property, where the latter has the advantage that it can be
`delete'd after use.
[...]
<script type="text/javascript">

var foo = function (){ alert('hi');};

this.foo = function ()
{
alert('hi');
};
</script>
</head>
<body>
<input type="button" onclick="foo();" value="Say 'hi'">


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top