...

View Full Version : why the code wont call on the function toogle_state?



guowei1000
06-11-2004, 04:38 AM
<html>
<head>
<script language ="JAVASCRIPT">
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.load("note.xml");

function TestChildren(nodes)
{
document.writeln("<table border = 1>");
document.writeln("<tr>");
document.writeln("<td>");
document.writeln(nodes.nodeName);

if (nodes.hasChildNodes)
{
for (var i=0; i <=nodes.childNodes.length-1; i++)
{
document.writeln("<table border = 1>");
document.writeln("<tr>");
document.writeln("<td>");
if (nodes.childNodes(i).hasChildNodes)
{
document.writeln("<font color= red><A onClick='javaScript:toogle_state(this)'>"+ nodes.childNodes(i).nodeName +"</A></font>");
document.writeln("</tr>");
document.writeln("</td>");
document.writeln("</table>");
}
else
{
document.writeln(nodes.childNodes(i).nodeName);
document.writeln("</tr>");
document.writeln("</td>");
document.writeln("</table>");
}

}
}


document.writeln("</td>");
document.writeln("</tr>");
document.writeln("</table>");
}

function toogle_state(counter)
{
alert("hi")
}


function callReadXml()
{
root = xmlDoc.documentElement;
TestChildren(root);
}
</script>
</head>
<body onLoad=callReadXml()>

</body>
</html>

arhh, shouldnt the function toogle_state be call when i click?? What wrong with the code?

glenngv
06-11-2004, 05:57 AM
If you execute document.write method AFTER the page has loaded, it will erase the current content and create a new document. So when the links are created, all your javascript functions are gone.

The solution is to call callReadXml() AS the page is loading, not on onload.

<body>
<script type="text/javascript">
callReadXml();
</script>
</body>

guowei1000
06-11-2004, 06:11 AM
:O thanks! its solved my problem!! :D



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum