SitePoint Sponsor

User Tag List

Results 1 to 9 of 9
  1. #1
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Javascript in Ajax responseText not parsed

    I have some JavaScript that makes an XMLHttpRequest which runs a PHP script. In the responseText, the PHP script should return some JavaScript and have that parsed.

    None of the JavaScript being sent back is being parsed. I can see that it's being passed with "Firebug", but it's just text in the html source.

    Does anyone know if this is possible? Let me know if this doesn't make sense and I'll elaborate.

    Thanks a lot.

  2. #2
    SitePoint Wizard Pepejeria's Avatar
    Join Date
    Jan 2005
    Location
    Too far up north
    Posts
    1,566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    How do you handle the response in the JavaScript code?

    Something like this perhaps?

    Code JavaScript:
    var response = eval('(' + ajax.responseText + ')');

    You might wanna wrap the above in a try/catch.

  3. #3
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I'm actually just populating a division innerHTML with the responseText. I'll give the eval() a try.

  4. #4
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    In the screenshot below you can see the response that Firebug outputs. The
    Code:
    <div id="content">
    is the division that's being populated by the
    Code:
    responseText
    . It's something like,

    Code:
    document.getElementById('content').innerHTML = http.responseText;
    The response is from a PHP script. I feel like this isn't going to be possible.

    Firebug screenshot

  5. #5
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Most ajax libraries have a property like "evalScripts=true" that you can trigger. If you're making your own ajax request then you'll need to use eval.

  6. #6
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jim, I'm a little confused on where the eval() should go though. Right now the PHP script is something like,

    PHP Code:
    <?php
    echo <<<eof
    <div>some content</div>
    <div>more content</div>
    <script type="text/javascript">
    alert('hi');
    </script>
    <div>content</div>
    eof;
    ?>
    The contents of reponseText will be,

    Code:
    <div>some content</div>
    <div>more content</div>
    <script type="text/javascript">
    alert('hi');
    </script>
    <div>content</div>
    Are you suggesting using eval inside the reponseText by making my PHP script something like,

    PHP Code:
    <?php
    echo <<<eof
    <div>some content</div>
    <div>more content</div>
    <script type="text/javascript">
    eval("alert('hi')");
    </script>
    <div>content</div>
    eof;
    or using the eval() on the responseText like,

    Code:
    document.getElementById('content').innerHTML = eval(http.responseText)
    Or maybe something completely different. Thanks again.

  7. #7
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok jim, I think I see what you're saying based on

    http://henrik.nyh.se/2006/11/prototy...-response-text

    If I use,

    Code:
    document.getElementById('content').innerHTML = eval(http.responseText);
    I get the error,

    Code:
    missing ; before statement
    [Break on this error] <script type="text/javascript">\n

  8. #8
    SitePoint Member
    Join Date
    Mar 2008
    Posts
    8
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Okay, I just solved my problem.

    In that last reply, the http.reponseText was a mixture of html and javascript and the eval() was going crazy. I just made a separate request where the reponseText is only JavaScript.

    Thanks for everyones help.

  9. #9
    SitePoint Wizard chris_fuel's Avatar
    Join Date
    May 2006
    Location
    Ventura, CA
    Posts
    2,750
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A bit late in the game but just in case.. If you're parsing JSON, instead of using eval, you can use the JSON JS library, which is a bit more particular when it comes to parsing the data. It will help with some of the nasties that you might come across. Some libraries may already do this, but I'm not sure which of them specifically.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •