SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Returning JS in Ajax using eval

    Hi all, I've got a the code returning fine in Ajax but now want to add a link with an onclick function. However, the javascript as show below doesn't work when returned via ajax but does work if I called the function directly!

    So, my code is:
    Code PHP:
    ...
     
      $objectid="Example value";
      ?>
      <script>var videocode="<? echo $objectid; ?>";</script>
      <a href="#" onclick="return clicker(videocode)">example</a>
      <?
     
    ...

    I've tried testing with the eval code but can't get it work. What am I doing wrong?

  2. #2
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    *friendly bump*

    As above, the javascript code above works fine if I don't use ajax to call the function! I read that using eval() would fix this but not entirely sure on what to do!

  3. #3
    Function Curry'er JimmyP's Avatar
    Join Date
    Aug 2007
    Location
    Brighton, UK
    Posts
    2,006
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Trying to load any JavaScript via XHR and then using eval() to run it should be avoided (even if you're using JSON it's still a bad idea). The best way to handle this problem would be to have unobtrusive JavaScript (no event attributes) which is already on the page. If you can't have it on the page then load it using traditional methods:

    E.g.

    Code JavaScript:
    var script = document.createElement('script');
    script.src = 'javascriptFile.js';
    document.body.appendChild(script);
    James Padolsey
    末末末末末末末末末末末末末末末末末末末
    Awesome JavaScript Zoomer (demo here)
    'Ajaxy' - Ajax integration solution (demo here)

  4. #4
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Jimmy for the response. Not quite sure what the solution is though! Do you mean create an js file with the script inside and then call that via PHP via an include? How do I then pass the variable to the onclick function ...?

  5. #5
    SitePoint Addict
    Join Date
    Dec 2007
    Posts
    207
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code looks fine, don't see what eval has to do with this.

    Also in the PHP part, since you're using shorttags you might as well use echo's short syntax:

    PHP Code:
      <script>var videocode="<?= $objectid?>";</script>
    mmj

  6. #6
    SitePoint Addict Bravogolf's Avatar
    Join Date
    Aug 2004
    Location
    All over really :)
    Posts
    205
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks both, I got it working today! I avoided creating a JS value for the onclick function call and instead echoed the PHP values into the function call directly using a htmlentities function (as it was a youtube object).

  7. #7
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Location
    Christchurch, New Zealand
    Posts
    14,729
    Mentioned
    104 Post(s)
    Tagged
    4 Thread(s)
    I think that he wants both the script and the link passed back to the page via ajax, which he then wants to use to update the page with and provide people a link to that script.

    Edit:

    Problem solved. Nice.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


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
  •