Ajax - evaluate scripts in response

Please support our JavaScript / DHTML / AJAX advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
Reply

Join Date: Jul 2006
Posts: 11
Reputation: AndrewSmith is an unknown quantity at this point 
Solved Threads: 0
AndrewSmith AndrewSmith is offline Offline
Newbie Poster

Ajax - evaluate scripts in response

 
0
  #1
Aug 23rd, 2006
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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="javaScript">
  2. function testFunction(){
  3. alert("HELLO WORLD");
  4. }
  5. testFunction();
  6. </script>
Should this function not execute, and alert "Hello world?"
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 75
Reputation: Sailor_Jerry is an unknown quantity at this point 
Solved Threads: 2
Sailor_Jerry's Avatar
Sailor_Jerry Sailor_Jerry is offline Offline
Junior Poster in Training

Re: Ajax - evaluate scripts in response

 
0
  #2
Aug 24th, 2006
Originally Posted by AndrewSmith View Post
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:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <script language="javaScript">
  2. function testFunction(){
  3. alert("HELLO WORLD");
  4. }
  5. testFunction();
  6. </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
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. //put your response in a div or something and use the div id to grab the tag with a name = to script
  2. divId.getElementsByTagName("script")
  3. loop...
  4. eval(script.innerHTML)

Running this after you response will run all the scripts on your page.
Last edited by Sailor_Jerry; Aug 24th, 2006 at 6:37 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the JavaScript / DHTML / AJAX Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC