Replace certain words on an entire page

C

chadhuntley

I've been trying to figure this out without much luck. It doesn't seem
like it should be to difficult, but I just can't even find where to
start.

What I'm trying to do is to have javascript look through an entire
page (its entire source), find all instances of a word I assign, and
not display that word.

What I was even thinking about doing was taking the entire source,
from <html> to </html>, placing it into a string, and somehow using
replace().

Does anyone have a script for this? Or walk me through it?
 
J

jamie.ly

I've been trying to figure this out without much luck. It doesn't seem
like it should be to difficult, but I just can't even find where to
start.

What I'm trying to do is to have javascript look through an entire
page (its entire source), find all instances of a word I assign, and
not display that word.

What I was even thinking about doing was taking the entire source,
from <html> to </html>, placing it into a string, and somehow using
replace().

Does anyone have a script for this? Or walk me through it?

document.getElementsByTagName('html')[0].innerHTML =
document.getElementsByTagName('html')[0].innerHTML.replace ( /myword/,
'my replacement' );

might work, or else try replace 'html' with 'body'
 
C

chadhuntley

document.getElementsByTagName('html')[0].innerHTML =
document.getElementsByTagName('html')[0].innerHTML.replace ( /myword/,
'my replacement' );

might work, or else try replace 'html' with 'body'


I ended up using 'body' and it worked out great. Thanks!
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]
oglegroups.com>, Wed, 1 Aug 2007 16:22:41, "(e-mail address removed)"
I've been trying to figure this out without much luck. It doesn't seem
like it should be to difficult, but I just can't even find where to
start.

What I'm trying to do is to have javascript look through an entire
page (its entire source), find all instances of a word I assign, and
not display that word.

What I was even thinking about doing was taking the entire source,
from <html> to </html>, placing it into a string, and somehow using
replace().

Does anyone have a script for this? Or walk me through it?

Here's a possible start, replacing "number"; but note that the code
replaces the word in itself too.

document.body.innerHTML =
document.body.innerHTML.replace(/number/g, "")

Tested briefly in IE6 js-quick only.

It's a good idea to read the newsgroup c.l.j and its FAQ. See below.
 
E

Evertjan.

Dr J R Stockton wrote on 01 aug 2007 in comp.lang.javascript:
Here's a possible start, replacing "number";

document.body.innerHTML =
document.body.innerHTML.replace(/number/g, "")
but note that the code
replaces the word in itself too.

Use:

document.body.innerHTML =
document.body.innerHTML.replace(/(num)ber/g, "")

===========

Warning: the above will also change:

<input name='numberEight'>
to
<input name='Eight'>

and even uncheck [IE7]:
<input type=checkbox name=number checked>
 

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
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top