Holla

I am using AJAX to load content into a section of my website. I would like the content that is loaded in to execute some javaScript. However, when i simply include it in the response, it doesn't get evaluated.
Say i request a page, test.htm. That page includes ONLY the following code:

<script language="javaScript">
  function testFunction(){
      alert("HELLO WORLD");
  }
  testFunction();
</script>

Should this function not execute, and alert "Hello world?"

Holla

I am using AJAX to load content into a section of my website. I would like the content that is loaded in to execute some javaScript. However, when i simply include it in the response, it doesn't get evaluated.
Say i request a page, test.htm. That page includes ONLY the following code:

<script language="javaScript">
  function testFunction(){
      alert("HELLO WORLD");
  }
  testFunction();
</script>

Should this function not execute, and alert "Hello world?"

No the function will not execute. If you want that to run the script you will need to call a function after your response to "eval" the script for you.

Your new function will need to find and execute the script in the DOM.

pseudo code below

//put your response in a div or something and use the div id to grab the tag with a name = to script
divId.getElementsByTagName("script")
loop...
eval(script.innerHTML)

Running this after you response will run all the scripts on your page.

This article has been dead for over six months. Start a new discussion instead.