SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Zealot
    Join Date
    Feb 2002
    Posts
    123
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    ajax and returning<script></script> inside return

    i've noticed with ajax lately that if you if get a response from your xml request with "<script>document.write('hello world')</script>" that none of the actions within the javascript aren't excecuted or performed. any way i could make this work?

  2. #2
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Something like this will never work:

    something.innerHTML = "<script>document.write('hello world')</script>"

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,408
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    Quote Originally Posted by jimfraser
    Something like this will never work:

    something.innerHTML = "<script>document.write('hello world')</script>"
    This works fine:

    Code:
    something.innerHTML = "<script>document.write('hello world')</script>";
    var x = something.getElementsByTagName("script");    
    for(var i=0;i<x.length;i++)   
    {
           eval(x[i].text);   
    }
    But when the script gets a bit more complicated, things start to go wrong. I have this counter object that counts backwards to zero. I basically create a new counter object, set the starting time, and then call the objects display function, which displays the remaining time, subtracts one second, waits .99 seconds and then calls itself. When the remaining time is less then 0, the function terminates.

    Loading it directly from the browser it works fine. But when i put it in an innerHTML, and use the eval-script above, it displays the remaining time, but doesn't count backwards. When the function tries to call itself, a javascript error appears that the object isn't defined.

    Any ideas on how to resolve this?

  4. #4
    SitePoint Wizard
    Join Date
    Nov 2004
    Location
    Nelson BC
    Posts
    2,310
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Maybe instead of eval you could do something like:

    Code:
    var myscript = "function doTimeout(){ alert('t/o'); };window.setInterval(doTimeout, 1000);";
    var func = new Function(myscript);
    func();

  5. #5
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,408
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    I resolved it not creating an object, but simply calling a function with the remaining time and destination div name as parameters, which then calls itself through setTimeout.

    So in the HTML that returns from the AJAX call I have:

    Code:
    showClock('cdtotal', 'clocktotal', 1000, escape('<a href=# onClick="manageContent(\'ships\', null);">Finito</a><br>'));
    which executes fine with eval.


  6. #6
    SitePoint Addict jtrelfa's Avatar
    Join Date
    Oct 2004
    Location
    Troy, Mi
    Posts
    231
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by guido2004
    Code:
    showClock('cdtotal', 'clocktotal', 1000, escape('<a href=# onClick="manageContent(\'ships\', null);">Finito</a><br>'));
    which executes fine with eval.
    I'm sure you're happy with the end result - but is 'eval' what you really want to use? eval, although a powerful function within JavaScript, should be used sparingly. My motto is typically: "If you're using eval, you shouldn't be".

    Here's a couple of websites regarding eval():

    http://24ways.org/advent/dont-be-eval
    http://thedailywtf.com/forums/thread/24081.aspx
    http://blogs.msdn.com/ericlippert/ar.../01/53329.aspx (yup - even microsoft doesn't recommend it unless necessary)

  7. #7
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,408
    Mentioned
    149 Post(s)
    Tagged
    4 Thread(s)
    I quote from the first link you gave:

    Some programmers say that eval() is B.A.D. Broken As Designed and should be removed from the language. However, there are some places in which it can dramatically simplify your code. A great example is for use with XMLHttpRequest, a component of the set of tools more popularly known as Ajax.
    Which is exactly the case


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
  •