When inserted in the source code, this program counts the "#text" nodes and returns the result.
But I don't thing this is the solution to this weeks homework because my program seems to find only the whitespace(which is of type #text) and not the actual text. So I will continue working on this.
Anyway, the result I get is 7 text nodes in the childnodes of <body>
<form style="text-align:center">
<input type="button" value="Click me!" onclick="countText()" />
</form>
<script>
function countText() {
var nr = 0;
var counter = 0;
for (nr=0;nr<500;nr++) {
var theHtmlNode = document.childNodes[1];
var theBodyNode = theHtmlNode.childNodes[1];
var theRest = theBodyNode.childNodes[nr];
if (theRest.nodeName == "#text") {
counter++;
}
else if (theRest.nodeName == "SCRIPT") {
break;
}
//alert(theRest.nodeName);/*this can be used to manualy go through the nodes to see what it actually finds.*/
}
alert(counter);
}
</script>
glenn.hellquist@gmail.com
webbkraft.com
/*#p2pu-Jan2011-javascript101*/
Inga kommentarer:
Skicka en kommentar