By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,598 Members | 1,894 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,598 IT Pros & Developers. It's quick & easy.

execute a javascript after an ajax response

P: 2
Dear coders,

I'm facing a problem and after few research I found that many ppl were looking around for the same solution, hope someone more expert than me could give a good help.

I created an AJAX function that i.e. on click does a request to a server and show into a specified tag the results (well until here is something eveyone would do with AJAX)... the point is that the result is something like the this:

i.e.

[HTML]<!-- this is the span tag where I'm going to wrap the ajax result -->
<span name="script2exe" id="script2exe">
some HTML...
<script type="text/javascript" language="javascript">
some JAVASCRIPT...
</script>
some HTML...
</span>[/HTML]

As we would expect the Javascript that will be received with the "innerHTML" will be not executed...

well as a beginner it was quite strange to me, but I understood that we need to weap the Javascript into a tag with id... than call it and eval whats wrapped in... I will show you what I did, and I tryed few different ways, but nothing!

Expand|Select|Wrap|Line Numbers
  1. //  this is the callback function to retrieve the ajax request
  2.  
  3. function alertContentsInit(http_request, tag_target) {
  4. if (http_request.readyState == 4) {
  5. if (http_request.status == 200) {
  6. result = http_request.responseText;
  7. //  with this I get all the response that a PHP page build
  8. document.getElementById(tag_target).innerHTML = result;
  9.  
  10. //  with this following two row, I try to get the script and execute it
  11. script2exe = document.getElementById("script2exe");
  12. eval(script2exe.getElementsByTagName("script").innerHTML);
  13. } else {
  14. alert(\'There was a problem...\');
  15. }
  16. }
  17. }

When I execute my page, nothing happen... the ajax result is loaded correctly, looking to the source code, I can see also the javascript well placed and correct builded (cos i tested it separately) but the eval... seems to be not correct (no errors received from the browser!)

thank you in advance for any help.
Aug 4 '08 #1
Share this Question
Share on Google+
3 Replies


acoder
Expert Mod 15k+
P: 16,027
Instead of eval-ing add the script to the head, e.g.
Expand|Select|Wrap|Line Numbers
  1. var head = document.getElementsByTagName("head")[0];
  2. var script = document.createElement("script");
  3. script.type="text/javascript";
  4. script.appendChild(document.createTextNode(str)); //str is the string containing script
  5. head.appendChild(script);
If you could use a file, then just set the src property instead.
Aug 4 '08 #2

P: 2
Instead of eval-ing add the script to the head, e.g.
Expand|Select|Wrap|Line Numbers
  1. var head = document.getElementsByTagName("head")[0];
  2. var script = document.createElement("script");
  3. script.type="text/javascript";
  4. script.appendChild(document.createTextNode(str)); //str is the string containing script
  5. head.appendChild(script);
If you could use a file, then just set the src property instead.

Thanx a lot acoder for your post, is an interesting way you are suggesting!

I modified my script and finally it is working, but using still the same method...

The problem now is that it works just in FF and not IE, maybe if you are more in into this things, could suggest me somithing looking though the code.

following the updated script:

Expand|Select|Wrap|Line Numbers
  1. function alertContentsInit(http_request, tag_target, url) {
  2. if (http_request.readyState == 4) {
  3. if (http_request.status == 200) {
  4.  
  5. result = http_request.responseText;
  6.  
  7. // with this I get all the response that a PHP page build
  8. document.getElementById(tag_target).innerHTML = result;
  9.  
  10. // with this following 3 rows, I try to get the script and execute it
  11. var ob = document.getElementsByTagName("script");
  12. for(var i=0; i<ob.length-1; i++){
  13. if(ob[i+1].text!=null) eval(ob[i+1].text);
  14. }
  15.  
  16. } else {
  17. alert(\'There was a problem\');
  18. }
  19. }
  20. }
Aug 5 '08 #3

acoder
Expert Mod 15k+
P: 16,027
The problem will probably be with the .text property. You may want to use innerHTML instead.
Aug 5 '08 #4

Post your reply

Sign in to post your reply or Sign up for a free account.