i use this code to get text of all nodes in body (Browser is IE8 it works on FF…)
thanks ,i develop a toolbar to change some texts in pages so if you have any other solution will made me happy,if i replace all of body with regex,it disable some futures of sites.for example in facebook if use replace all suggestion future will disable in friend finds section,so i want to change words step by step,it’s a filter for bad words
function replaceText(node)
{
var textPropName = node.textContent === undefined ? 'innerText' : 'textContent';
var childs = node.childNodes, i = 0;
while(i < childs.length)
{
alert(childs[i][textPropName]);
if (childs[i][textPropName] !=null)
{
childs[i][textPropName] =childs[i][textPropName].replace(rgx,'new');
}
else
{
replaceText(document.body.childNodes[i]);} i++;
}
}
}
it’s work and i can replace all text on a page but if a text be in out of tags same as belove i can’t access to it and replace it
problem: test5 ww tt xx test aren’t in any tag so they don’t replaced
any solution?
sincerely
ww tt ff xx test5 test
<b>
test old old yyyl yyy ppp
<h2> ppp gfdsgfds gf dg df yyy old odld old</h2>
</b>
<h1 id="myHeader" onClick="replaceText(document.body)">
yyy yyy yyy yyy old Click me! whatever
</h1>
ppp ppp ppp bwhoreface
<b>
ppp
end solution
function replaceText(node){
var textPropName = node.textContent === undefined ? 'innerText' : 'textContent';
var childs = node.childNodes, i = 0;
var len= node.childNodes.length;
// alert('len='+len);
//var rgx = new RegExp('\\bold\\b', 'gi');
while(node = childs[i]){
//alert('ee'+node.nodeType);
if (node.nodeType == 3 ) {
//alert('!!!!!'+node.nodeValue );
//alert('ee'+node.nodeType);
childs[i].nodeValue = childs[i].nodeValue.replace(rgx, 'newText');
} else {
replaceText(node);
}
Here you go, this works in IE too…
Live Demo