change text: foo -> bar

D

Dan Diebolt

I need some script that change "foo" to "bar" anywhere "foo" occurs on
the page once the page is loaded. Any idea how?
 
E

Evertjan.

Dan Diebolt wrote on 04 sep 2003 in comp.lang.javascript:
I need some script that change "foo" to "bar" anywhere "foo" occurs on
the page once the page is loaded. Any idea how?

IE:

<body onload='mybody.innerText=mybody.innerText.replace(/foo/g,"bar")'>
<div id="mybody">
blah foo<br>
blah foo
</div>
</body>
 
D

Dan Diebolt

<body onload='mybody.innertext=mybody.innertext.replace(/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>.
 
E

Evertjan.

Dan Diebolt wrote on 04 sep 2003 in comp.lang.javascript:
<body onload='mybody.innertext=mybody.innertext.replace(/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>.

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".
 
L

Lasse Reichstein Nielsen

I need some script that change "foo" to "bar" anywhere "foo" occurs on
the page once the page is loaded. Any idea how?

Best guess is to run through the DOM tree:
---
function crawlDOM(node,fnode,ftext) {
switch (node.nodeType) {
case 1: // normal node
for(var i = 0;i<node.childNodes.length; i++) {
crawlDOM(node.childNodes,fnode,ftext);
}
fnode(node);
break;
case 3: // text node
ftext(node);
break;
}
}
---

Then you can do:
---
crawlDOM(document.body,
function (){},
function (tnode) {
tnode.nodeValue = tnode.nodeValue.replace(/foo/g,"bar");
});
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,083
Messages
2,570,591
Members
47,212
Latest member
RobynWiley

Latest Threads

Top