...

View Full Version : Trouble with eval()



EdwardKing
09-21-2005, 09:36 AM
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!

glenngv
09-21-2005, 09:52 AM
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


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


...
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>';
...



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum