Site Navigation
Replacing characters throughout the page
written by vegu on 31 Dec, 2006 01:58:53
A member over at
CodingForums had a problem where he needed to replace certain characters in his page using javascript.
I usually dont copy the stuff i post over at coding forums to this page, but here is one solution to his problem.
We create a function that allows us to recursively loop through all the elements on the page. We will call this function scanNodes.
function scanNodes(node) {
var i;
if(node.nodeName == '#text') {
var oldText = node.nodeValue;
var newNode = document.createTextNode(oldText.replace(/a/g, 'A'));
node.parentNode.insertBefore(newNode, node);
node.parentNode.removeChild(node);
return;
}
for(i = 0; i < node.childNodes.length; i++)
scanNodes(node.childNodes[i]);
}In order to get the replacement process started simply call the scanNodes function on the body element when the page has loaded.
<body onload="scanNodes(document.body);">
Related Posts
Your Comment
Comments
No comments yet.