Dan Diebolt wrote on 04 sep 2003 in comp.lang.javas cript:
[color=blue]
> I need some script that change "foo" to "bar" anywhere "foo" occurs on
> the page once the page is loaded. Any idea how?
>[/color]
Dan Diebolt wrote on 04 sep 2003 in comp.lang.javas cript:
[color=blue]
> <body onload='mybody. innertext=mybod y.innertext.rep lace(/foo/g,"bar")'>
> <div id="mybody">
> blah foo<br>
> blah foo
> </div>
> </body>
>
> I don't have a <div> to grab "mybody" as a handle. I have to simply make
> the change to whatever text is within <body>.
>
>[/color]
1/ It is not innertext, but innerText
2/ Why do you let yourself be constricted that way ? It seems you want to
change somone elses work. This could be unetical. The final p[ossibilitiy
is called "html-mining".
--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress)
dandiebolt@yaho o.com (Dan Diebolt) writes:
[color=blue]
> I need some script that change "foo" to "bar" anywhere "foo" occurs on
> the page once the page is loaded. Any idea how?[/color]
Best guess is to run through the DOM tree:
---
function crawlDOM(node,f node,ftext) {
switch (node.nodeType) {
case 1: // normal node
for(var i = 0;i<node.childN odes.length; i++) {
crawlDOM(node.c hildNodes[i],fnode,ftext);
}
fnode(node);
break;
case 3: // text node
ftext(node);
break;
}
}
---
Then you can do:
---
crawlDOM(docume nt.body,
function (){},
function (tnode) {
tnode.nodeValue = tnode.nodeValue .replace(/foo/g,"bar");
});
---
You just have to pray that the document tree really is a tree :)
/L
--
Lasse Reichstein Nielsen - lrn@hotpop.com
Art D'HTML: <URL:http://www.infimum.dk/HTML/randomArtSplit. html>
'Faith without judgement merely degrades the spirit divine.'
Comment