Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New Coder
    Join Date
    Jun 2005
    Posts
    44
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Trouble with eval()

    I have two files like follows:

    test1.html
    <iframe width="100%" height="100" id="hiddenframe"></iframe>
    <script>
    function ExpandNode(ParentNode){
    var NodeX=eval(ParentNode.id+'_0');
    alert(NodeX);
    document.frames['hiddenframe'].location.replace("test2.html");
    }
    function ExpandNode11(ParentNode){
    var NodeX=eval(ParentNode.id+'_1');
    alert(NodeX);
    document.frames['hiddenframe'].location.replace("test2.html");
    }
    <div id='Node_0'><img src="tree_spread1.gif" onClick='ExpandNode(Node_0)'>
    <a href='#' onDblClick='ExpandNode(Node_0)'><font class='CLink'>item1</font></a>
    </div>
    <div id='Node_0_0' style='display: none' loaded='no'>
    <font class='CLink'>testing</font>
    </div>

    test2.html
    <html>
    <body>
    <script>
    var x = eval("parent.Node_0_0");
    x.innerHTML="<div id=′Node_2′> <img src='tree_spread2.gif' onClick='ExpandNode11(Node_2)'><font class='CLink'>&nbsp;子项11</font></div>";
    x.innerHTML=x.innerHTML+"<div id='Node_2_1' style='display: none' loaded='no'><font class='CLink'>&nbsp;&nbsp;正在加载1 ...</font></div>";
    </script>
    </body>
    </html>

    when I call ExpandNode11 function from test2.html,it has nothing action.Then I modify test2.html
    <html>
    <body>
    <script>
    var x = eval("parent.Node_0_0");
    x.innerHTML="<div id=′Node_2′> <img src='tree_spread2.gif' onClick='ExpandNode11(Node_2_1)'><font class='CLink'>&nbsp;item11</font></div>";
    x.innerHTML=x.innerHTML+"<div id='Node_2_1' style='display: none' loaded='no'><font class='CLink'>test</font></div>";
    </script>
    </body>
    </html>

    and ExpandNode11 in test1.html
    function ExpandNode11(ParentNode){
    var NodeX=eval(ParentNode.id);
    alert(NodeX);
    document.frames['hiddenframe'].location.replace("test2.html");
    }

    when I call ExpandNode11 function from test2.html,it has action. I am puzzled with it!
    ExpandNode11(Node_2) --> var NodeX=eval(ParentNode.id+'_1');
    ExpandNode11(Node_2_1) --> var NodeX=eval(ParentNode.id);


    I think above two sentence has same function,why it don't have same function in fact?

    Please help!

  • #2
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    You don't need eval in the first place. You should properly reference elements by document.getElementById and not directly by id. That bad practice only works in IE.
    test1.html
    Code:
    function ExpandNode(parentNodeId){
      var parentNode = document.getElementById(parentNodeId);
      alert(parentNode.innerHTML); //test
      var NodeX = document.getElementById(parentNodeId+'_0');
      alert(NodeX.innerHTML); //test
      window.frames['hiddenframe'].location.replace("test2.html");  
    }
    ...
    <div id="Node_0"><img src="tree_spread1.gif" onClick="ExpandNode('Node_0')" />
    ...
    test2.html
    Code:
    ...
    var x = parent.document.getElementById("Node_0_0"); 
    x.innerHTML='<div id="Node_2"><img src="tree_spread2.gif" onclick="parent.ExpandNode11(\'Node_2\')"><span class="CLink">&nbsp;子项11</span></div>' +
                       '<div id="Node_2_1" style="display: none" loaded="no"><span class="CLink">&nbsp;&nbsp;正在加载1 ...</span></div>';
    ...
    Last edited by glenngv; 09-21-2005 at 09:04 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________


  •  

    Posting Permissions

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