How to dynamically add a Javascript function (and invoke)

Posted by whitey on Stack Overflow See other posts from Stack Overflow or by whitey
Published on 2010-06-10T12:57:25Z Indexed on 2010/06/10 13:02 UTC
Read the original article Hit count: 156

Filed under:
|
|

Based on a click event on the page, via ajax I fetch a block of html and script, I am able to take the script element and append it to the head element, however WebKit based browsers are not treating it as script (ie. I cannot invoke a function declared in the appended script).

Using the Chrome Developer Tools I can see that my script node is indeed there, but it shows up differently then a script block that is not added dynamically, a non-dynamic script has a text child element and I cannot figure out a way to duplicate this for the dynamic script.

Any ideas or better ways to be doing this? The driving force is there is potentially a lot of html and script that would never be needed unless a user clicks on a particular tab, in which case the relevant content (and script) would be loaded. Thanks!

© Stack Overflow or respective owner

Related posts about JavaScript

Related posts about AJAX