Forum Moderators: open

Message Too Old, No Replies

ativating javascript after an ajax call

         

fintan

12:53 pm on Aug 1, 2006 (gmt 0)

10+ Year Member



Hi I'm in the process of building an ajax form. I have a page that's filled with results & when a user clicks on one of the results.

The result expands & is filled with a form. In this form is javascript. When I try to use the javascript nothing happens.

The javascript is correct but I can't activate it. Is there a way to get around this? Thanks

fintan

RonPK

8:58 pm on Aug 1, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are you adding the script by using innerHTML? I'm asking because I recall that the proper way to do things like this is by adding the script to the DOM, by using createElement() and appendChild().

fintan

9:45 am on Aug 2, 2006 (gmt 0)

10+ Year Member



Thanks RonPK for the reply. Yeah I'm using innerHTML to fill out the div. Can you create javascript using them methods?

RonPK

1:00 pm on Aug 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah it should. Try this, as an example:

<script type="text/javascript"> 
function addscript() {
var newScript = 'alert("it works!")';
var jsEl = document.createElement('script');
jsEl.type = 'text/javascript';
jsEl.appendChild(document.createTextNode(newScript));
document.body.appendChild(jsEl);
}
</script>
<input type="button" value="try me" onclick="addscript()">

Pressing the button adds a script element to the DOM. Unfortunately IE throws an error... Let's hope some guru stops by to give some proper advise :(