replacing text in a page

P

prw

Hi. I'm trying to replace some text in a page via the location bar (in
IE or Firefox). When I type in:

javascript:document.body.innerHTML=document.body.innerHTML.replace("oldtext","newtext");

The text is changed, but all formatting is lost also! Is there a way
around this?

Cheers,

prw404
 
L

Lee

prw said:
Hi. I'm trying to replace some text in a page via the location bar (in
IE or Firefox). When I type in:

javascript:document.body.innerHTML=document.body.innerHTML.replace("oldtext","newtext");

The text is changed, but all formatting is lost also! Is there a way
around this?

Your question doesn't make sense.
You aren't really using "oldtext" and "newtext" as your
parameters, are you? Does whatever you're really specifying
as the replacement text contain formatting?


--
 
E

Evertjan.

Lee wrote on 05 jul 2007 in comp.lang.javascript:
prw said:

Your question doesn't make sense.
You aren't really using "oldtext" and "newtext" as your
parameters, are you? Does whatever you're really specifying
as the replacement text contain formatting?

.... and

javascript replace expects a regex object as the first parameter.

..replace(/oldtext/,"newtext");

.... and

favelets shoud not return anything,
so should be closed by appending something like:

;void 0;
 
R

RobG

prw said:






Your question doesn't make sense.

I think the question makes sense, it just isn't a sensible thing to
do. :)

You aren't really using "oldtext" and "newtext" as your
parameters, are you? Does whatever you're really specifying
as the replacement text contain formatting?

Note to the OP: the replacement will occur in HTML tags, attribute
names and their values as well as the actual content of the elements.
e.g.: if you were to use your method to replace all the instances of
'e' with 'i' say using:

...innerHTML = ...innerHTML.replace(/e/ig,'i');

in the following:

<p style="color: red;">Hello world!</p>

you get:

<p styli="color: rid;">Hillo world!</p>

Extend that effect to an entire page and I think you see what's
happening.
 
D

Dr J R Stockton

In comp.lang.javascript message <[email protected]>
javascript replace expects a regex object as the first parameter.

Expects, perhaps; but not requires. See ISO/IEC 16262 15.5.4.11 para 3.
Try S = "abcabcabc".replace("b", 2+2) ; .

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

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,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top