Forum Moderators: open

Message Too Old, No Replies

Javascript within ajax reponse

running javascript function within ajax reponse

         

dave_c00

2:16 pm on Sep 17, 2008 (gmt 0)

10+ Year Member



Hi,

I am filling a div with some data using ajax and would like to include some javascript within it. The response would look like this:


<script type="text/javascript">
<!--
function show() {
alert('OI-2!');
}
-->
</script>
<input type="button" onclick="alert('OI-1!');show();" value="Alert" />

It seems to fill the div fine and when the button is pushed the first alert works but not the second.

Any ideas?

Thanks,

Dave

httpwebwitch

12:39 am on Sep 19, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



When loading content via AJAX, <script> tags don't execute. So, your show() function is not being defined.

For your script to execute upon arrival, you'll need to parse out the contents of the <script> tags and pass that string into eval() to execute it.

Typically you'd use a function like getElementsByTagName('script') to find the scripts in the response, grab their innerText.