Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

AJAX not executing javascript??

Status
Not open for further replies.

CamaroLT

Programmer
Dec 16, 2002
159
0
0
CA
I've got a DIV that is being populated by an AJAX call successfully. The data returned from the AJAX call gets populated into the DIV properly, however, no javascript code seems to execute. For example, if the AJAX calls simply returns the following:

Code:
Calling alert: <script>alert('Booyeah!')</script>

Not a thing happens.

I've tried in FF3.6 as well as Chrome and neither seem to want to throw the alert. Chrome does tell me that the script DOES get dumped into the DIV.

I'm stuck!

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
NEVER send the boss to do a techs job
 
How are you performing the AJAX call? Are you using one of the many frameworks (e.g. jQuery, Prototype, etc)?

If the answer is 'yes', you'll need to check the docs for whichever framework it is you are using, as some have special options for evaluating script code.

If you are not using a framework, then you should run your return string through "eval", first removing the <script> tags.

Hope this helps,

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
No framework, so Eval is a great suggestion.

What I am returning from the call is all HTML which is to be rendered in the DIV I have setup. So is it possible cut the string up, spit out the first half of the result, then run eval, then spit out the rest of the HTML?

The result I'm getting back that concerns me is like:

Code:
<td>Birthday</td><td><script>new tCal();</script></td>
[/code>

So what I'd do is sniff for <script> send out from the beginning of <td> to the end of the 2nd <td> then run eval, then send out </td>?

-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=-=*=
[b]NEVER[/b] send the boss to do a techs job
 
What I am returning from the call is all HTML which is to be rendered in the DIV I have setup.

So nothing like the example string you gave, then... still it shouldn't be a problem. As you say, you can simply strip the <script> out, and eval if after you've added your HTML into the DOM.

Dan



Coedit Limited - Delivering standards compliant, accessible web solutions

Dan's Page [blue]@[/blue] Code Couch:
Code Couch Snippets & Info:
The Out Atheism Campaign
 
Can you put the javascript you want executed in the AJAX script?

ie

{if (xmlhttp.readyState==4)
{document.getElementById("div_you_are_changing_content_on").innerHTML=xmlhttp.responseText;
additional script element here;
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top