JS 'eval' function issue

Discussion in 'JavaScript' started by dorinp, Jun 28, 2008.

  1. #1
    Hi,
    Have you deal with this 'eval' function issue ?
    In the JS code below - option a) work well but the b) is not working.
    Have you any idea why? It's this a JS bug ?
    I need this for having a line like: eval('document.all.counter_' + param1 + '.innerHTML') = 123;
    Thanks,
    Dorin

    //--------JS script----------
    function test()
    {
    html_code = '<div id="mydiv"></div>';
    document.write(html_code);

    // -- a)
    document.all.mydiv.innerHTML = 123;
    // -- b)
    eval('document.all.mydiv.innerHTML') = 123;
    }
     
    dorinp, Jun 28, 2008 IP
  2. koolman

    koolman Peon

    Messages:
    76
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you may try:
     
    koolman, Jun 28, 2008 IP
  3. Nafai

    Nafai Peon

    Messages:
    105
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    eval('document.all.mydiv.innerHTML') = 123;
    Code (markup):
    This doesn't work because document.all.mydiv.innerHTML is a pointer to the innerHTML property of the mydiv object. When you call eval('document.all.mydiv.innerHTML'), you get back the CONTENTS of the mydiv, returned as a string. The value returned is literally the string 'whatever html here'. When you try to set THAT STRING equal to something else, you are trying to change the string only, at that point it has no idea that it WAS the innerHTML of a div, all it knows is it is a string.

    But you can do it like this:
    eval('document.all.mydiv').innerHTML = 123;
    Code (markup):
     
    Nafai, Jun 29, 2008 IP