Current location: Hot Scripts Forums » General Web Coding » JavaScript » Running Javascript code returned form an AJAX call


Running Javascript code returned form an AJAX call

Reply
  #1 (permalink)  
Old 09-10-09, 08:58 PM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Running Javascript code returned form an AJAX call

It's a well-known issue: if you make an AJAX call and the returned HTML contains javascript, the javascript in the returned HTML won't run or work because it isn't reparsed into the page.

I've looked at numerous pages on the web related to solving this issue, but none of them are clear to me (or clear enough for me to understand, at least).

Currently I'm building an application that makes an AJAX call, and part of the returned code contains some javascript. Unfortunately the returning HTML it *has* to contain this javascript, there isn't any way around it that I can see.

Can anyone show me a clear example of how to overcome or solve this issue?
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #2 (permalink)  
Old 09-11-09, 06:12 AM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
My approach has been to embed the javascript in the calling page, or one included with a <script> tag, then add some indicator in the HTML that identifies which javascript to execute.

When the response is received from the server, the HTML is scanned to check for the indicator and the appropriate javascript is executed. An empty anchor tag works well. I've also used the text returned from a background script to determine whether it executed successfully.
Reply With Quote
  #3 (permalink)  
Old 09-11-09, 07:38 PM
JavaScriptBank JavaScriptBank is offline
New Member
 
Join Date: Jul 2009
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
I encountered the same problem in the past, and I used many solutions but still didn't solve it


Then I use jQuery, all everything are ok with a little code
Reply With Quote
  #4 (permalink)  
Old 09-11-09, 10:43 PM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Is there anyway you can use document.write to your advantage here? I do not really know if willl work in your situation but I know I wrote out html with javascript inside it dynamically using it. In order for the script process correctly though you need make sure you break apart your "<script>" tags to be like "<scr" + "ipt>" and your "</script>" to be "</scr" + "ipt>". you can do this using a replace method with minmal effort.

I am not sure this will work in your situation just throwing it out there.
Reply With Quote
  #5 (permalink)  
Old 09-13-09, 09:22 AM
End User's Avatar
End User End User is offline
Level II Curmudgeon
 
Join Date: Dec 2004
Posts: 3,027
Thanks: 14
Thanked 35 Times in 33 Posts
Quote:
Originally Posted by carters-site View Post
I am not sure this will work in your situation just throwing it out there.
This isn't really what the issue is about, but thanks anyway. The problem is that AJAX-generated javascript code sent back isn't linked to the DOM or automatically re-parsed and executed.
__________________
I don't live on the edge, but sometimes I go there to visit.
-------------------------------------------------------------------------
Sanitize Your Data | Oracle Date & Substring Functions | Code Snippet Library | [url=http://www.codmb.com/Call Of Duty[/url]
Reply With Quote
  #6 (permalink)  
Old 09-14-09, 02:17 PM
carters-site's Avatar
carters-site carters-site is offline
Wannabe Coder
 
Join Date: Sep 2009
Location: Moline, IL
Posts: 100
Thanks: 2
Thanked 1 Time in 1 Post
Sorry I must have misread your problem.

In my situation where I encountered it dealt with serving ads that can be any fragment of html and or javascript that has to be run on the page when it loads. I do not know which ad I am going to load because it is randomly determined in the so that was what I had to do to make the ad code run into the page.
Reply With Quote
  #7 (permalink)  
Old 09-14-09, 02:34 PM
wirehopper's Avatar
wirehopper wirehopper is offline
-
 
Join Date: Feb 2006
Posts: 2,515
Thanks: 20
Thanked 109 Times in 106 Posts
Can you post a snippet of the code? Or PM it?
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Space Above Form xavier039 CSS 1 07-13-09 10:52 PM
checkCheckboxGroup broken after form consolidation ski_woman JavaScript 0 01-12-05 11:00 AM
javascript solution for php/mysql online form jacobo JavaScript 0 06-04-04 04:21 PM
asp: URGENT! need to change code to create new form per id seala ASP 2 09-09-03 09:54 PM
javascript form dave111 JavaScript 2 08-21-03 12:36 AM


All times are GMT -5. The time now is 01:54 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.