execute a javascript after an ajax response

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pablosuk78
    New Member
    • Aug 2008
    • 2

    execute a javascript after an ajax response

    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="script2ex e" id="script2exe" >
    some HTML...
    <script type="text/javascript" language="javas cript">
    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!

    [CODE=javascript]// this is the callback function to retrieve the ajax request

    function alertContentsIn it(http_request , tag_target) {
    if (http_request.r eadyState == 4) {
    if (http_request.s tatus == 200) {
    result = http_request.re sponseText;
    // with this I get all the response that a PHP page build
    document.getEle mentById(tag_ta rget).innerHTML = result;

    // with this following two row, I try to get the script and execute it
    script2exe = document.getEle mentById("scrip t2exe");
    eval(script2exe .getElementsByT agName("script" ).innerHTML);
    } else {
    alert(\'There was a problem...\');
    }
    }
    }[/CODE]


    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.
    Last edited by gits; Aug 4 '08, 11:06 AM. Reason: added code tags
  • acoder
    Recognized Expert MVP
    • Nov 2006
    • 16032

    #2
    Instead of eval-ing add the script to the head, e.g.
    [code=javascript]var head = document.getEle mentsByTagName( "head")[0];
    var script = document.create Element("script ");
    script.type="te xt/javascript";
    script.appendCh ild(document.cr eateTextNode(st r)); //str is the string containing script
    head.appendChil d(script);[/code]If you could use a file, then just set the src property instead.

    Comment

    • pablosuk78
      New Member
      • Aug 2008
      • 2

      #3
      Originally posted by acoder
      Instead of eval-ing add the script to the head, e.g.
      [code=javascript]var head = document.getEle mentsByTagName( "head")[0];
      var script = document.create Element("script ");
      script.type="te xt/javascript";
      script.appendCh ild(document.cr eateTextNode(st r)); //str is the string containing script
      head.appendChil d(script);[/code]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:

      [CODE=javascript]function alertContentsIn it(http_request , tag_target, url) {
      if (http_request.r eadyState == 4) {
      if (http_request.s tatus == 200) {

      result = http_request.re sponseText;

      // with this I get all the response that a PHP page build
      document.getEle mentById(tag_ta rget).innerHTML = result;

      // with this following 3 rows, I try to get the script and execute it
      var ob = document.getEle mentsByTagName( "script");
      for(var i=0; i<ob.length-1; i++){
      if(ob[i+1].text!=null) eval(ob[i+1].text);
      }

      } else {
      alert(\'There was a problem\');
      }
      }
      }[/CODE]
      Last edited by acoder; Aug 5 '08, 03:42 PM. Reason: Added [code] tags

      Comment

      • acoder
        Recognized Expert MVP
        • Nov 2006
        • 16032

        #4
        The problem will probably be with the .text property. You may want to use innerHTML instead.

        Comment

        Working...