Connecting Tech Pros Worldwide Forums | Help | Site Map

replace instances of text on page with image or other text?

juglesh
Guest
 
Posts: n/a
#1: Jul 23 '05
Hello,

I need to look through the text on a page and replace certain words with an
image or other word

something like:

read document
find all instances of the word "blah"
change all instances of the word "blah" to <img src="MyPicture.jpg" >


(jeez, I don't get why computers cant just read english!<G>)
and, no, I cant just change the page with 'find/replace' in notepad.

--
thanks,
juglesh B>{)}



McKirahan
Guest
 
Posts: n/a
#2: Jul 23 '05

re: replace instances of text on page with image or other text?


"juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
news:Sj_xd.302269$R05.120039@attbi_s53...[color=blue]
> Hello,
>
> I need to look through the text on a page and replace certain words with[/color]
an[color=blue]
> image or other word
>
> something like:
>
> read document
> find all instances of the word "blah"
> change all instances of the word "blah" to <img src="MyPicture.jpg" >
>
>
> (jeez, I don't get why computers cant just read english!<G>)
> and, no, I cant just change the page with 'find/replace' in notepad.
>
> --
> thanks,
> juglesh B>{)}
>
>[/color]

It is not clear -- who's page?

For any given URL do you want to change "certain words" and display it or
what?


juglesh
Guest
 
Posts: n/a
#3: Jul 23 '05

re: replace instances of text on page with image or other text?



"McKirahan" <News@McKirahan.com> wrote in message
news:7C_xd.268087$HA.48168@attbi_s01...[color=blue]
> "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
> news:Sj_xd.302269$R05.120039@attbi_s53...[color=green]
>> Hello,
>>
>> I need to look through the text on a page and replace certain words with[/color]
> an[color=green]
>> image or other word
>>
>> something like:
>>
>> read document
>> find all instances of the word "blah"
>> change all instances of the word "blah" to <img src="MyPicture.jpg" >
>>
>>
>> (jeez, I don't get why computers cant just read english!<G>)
>> and, no, I cant just change the page with 'find/replace' in notepad.
>>
>> --
>> thanks,
>> juglesh B>{)}
>>
>>[/color]
>
> It is not clear -- who's page?[/color]

well, my .php is being included in their page(which they made for me) . my
..php page contains the head and the body tag, so i can put in a javascript,
and call it from body onload. basically, when it gets to your browser, you
see my head content, and most of the body is theirs.
[color=blue]
>
> For any given URL do you want to change "certain words" and display it or
> what?[/color]

so, not any given url, just on the pages where my head content is used.

their page is:
<?php include 'http://www.mydomain.com/head.php'; ?>
<p> Welcome. Content is here. blah and blah</p>
</body>

my head.php is
<head>
<my javascript>
</head>
<body>

i want my js to change all instances of 'blah' to <img src="MyPicture.jpg" >

which would change the output to:
Welcome. Content is here. <the image> and <the image>




McKirahan
Guest
 
Posts: n/a
#4: Jul 23 '05

re: replace instances of text on page with image or other text?


"juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
news:lZ_xd.689446$mD.299515@attbi_s02...[color=blue]
>
> "McKirahan" <News@McKirahan.com> wrote in message
> news:7C_xd.268087$HA.48168@attbi_s01...[color=green]
> > "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
> > news:Sj_xd.302269$R05.120039@attbi_s53...[color=darkred]
> >> Hello,
> >>
> >> I need to look through the text on a page and replace certain words[/color][/color][/color]
with[color=blue][color=green]
> > an[color=darkred]
> >> image or other word
> >>
> >> something like:
> >>
> >> read document
> >> find all instances of the word "blah"
> >> change all instances of the word "blah" to <img src="MyPicture.jpg"[/color][/color]
>[color=green][color=darkred]
> >>
> >>
> >> (jeez, I don't get why computers cant just read english!<G>)
> >> and, no, I cant just change the page with 'find/replace' in notepad.
> >>
> >> --
> >> thanks,
> >> juglesh B>{)}
> >>
> >>[/color]
> >
> > It is not clear -- who's page?[/color]
>
> well, my .php is being included in their page(which they made for me) .[/color]
my[color=blue]
> .php page contains the head and the body tag, so i can put in a[/color]
javascript,[color=blue]
> and call it from body onload. basically, when it gets to your browser,[/color]
you[color=blue]
> see my head content, and most of the body is theirs.
>[color=green]
> >
> > For any given URL do you want to change "certain words" and display it[/color][/color]
or[color=blue][color=green]
> > what?[/color]
>
> so, not any given url, just on the pages where my head content is used.
>
> their page is:
> <?php include 'http://www.mydomain.com/head.php'; ?>
> <p> Welcome. Content is here. blah and blah</p>
> </body>
>
> my head.php is
> <head>
> <my javascript>
> </head>
> <body>
>
> i want my js to change all instances of 'blah' to <img src="MyPicture.jpg"
>
>
> which would change the output to:
> Welcome. Content is here. <the image> and <the image>
>[/color]

Will this work? Watch for word-wrap.

<head>
<script type="text/javascript">
function MyHead() {
var find = "blah and blah";
var repl = "<img src='MyPicture.jpg'>";
var page = document.body.innerHTML;
while (page.indexOf(find) >= 0) {
var i = page.indexOf(find);
var j = find.length;
page = page.substr(0,i) + repl + page.substr(i+j);
document.body.innerHTML = page;
}
}
</script>
</head>
<body onload="MyHead()">


McKirahan
Guest
 
Posts: n/a
#5: Jul 23 '05

re: replace instances of text on page with image or other text?


make that:

var find = "blah";


juglesh
Guest
 
Posts: n/a
#6: Jul 23 '05

re: replace instances of text on page with image or other text?



"McKirahan" <News@McKirahan.com> wrote in message
news:Wd0yd.540796$wV.495762@attbi_s54...[color=blue]
> "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
> news:lZ_xd.689446$mD.299515@attbi_s02...[color=green]
>>
>> "McKirahan" <News@McKirahan.com> wrote in message
>> news:7C_xd.268087$HA.48168@attbi_s01...[color=darkred]
>> > "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
>> > news:Sj_xd.302269$R05.120039@attbi_s53...
>> >> Hello,
>> >>
>> >> I need to look through the text on a page and replace certain words[/color][/color]
> with[color=green][color=darkred]
>> > an
>> >> image or other word
>> >>
>> >> something like:
>> >>
>> >> read document
>> >> find all instances of the word "blah"
>> >> change all instances of the word "blah" to <img
>> >> src="MyPicture.jpg"[/color]
>>[color=darkred]
>> >>
>> >>
>> >> (jeez, I don't get why computers cant just read english!<G>)
>> >> and, no, I cant just change the page with 'find/replace' in notepad.
>> >>
>> >> --
>> >> thanks,
>> >> juglesh B>{)}
>> >>
>> >>
>> >
>> > It is not clear -- who's page?[/color]
>>
>> well, my .php is being included in their page(which they made for me) .[/color]
> my[color=green]
>> .php page contains the head and the body tag, so i can put in a[/color]
> javascript,[color=green]
>> and call it from body onload. basically, when it gets to your browser,[/color]
> you[color=green]
>> see my head content, and most of the body is theirs.
>>[color=darkred]
>> >
>> > For any given URL do you want to change "certain words" and display it[/color][/color]
> or[color=green][color=darkred]
>> > what?[/color]
>>
>> so, not any given url, just on the pages where my head content is used.
>>
>> their page is:
>> <?php include 'http://www.mydomain.com/head.php'; ?>
>> <p> Welcome. Content is here. blah and blah</p>
>> </body>
>>
>> my head.php is
>> <head>
>> <my javascript>
>> </head>
>> <body>
>>
>> i want my js to change all instances of 'blah' to <img
>> src="MyPicture.jpg"
>>
>>
>> which would change the output to:
>> Welcome. Content is here. <the image> and <the image>
>>[/color]
>
> Will this work? Watch for word-wrap.
>
> <head>
> <script type="text/javascript">
> function MyHead() {
> var find = "blah and blah";
> var repl = "<img src='MyPicture.jpg'>";
> var page = document.body.innerHTML;
> while (page.indexOf(find) >= 0) {
> var i = page.indexOf(find);
> var j = find.length;
> page = page.substr(0,i) + repl + page.substr(i+j);
> document.body.innerHTML = page;
> }
> }
> </script>
> </head>
> <body onload="MyHead()">[/color]

almost works! if i change repl to a different word, it will work (it will
display another word), but if i use that img tag, nothing changes.




juglesh
Guest
 
Posts: n/a
#7: Jul 23 '05

re: replace instances of text on page with image or other text?



"juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
news:Lf1yd.612732$D%.496186@attbi_s51...[color=blue]
>
> "McKirahan" <News@McKirahan.com> wrote in message
> news:Wd0yd.540796$wV.495762@attbi_s54...[color=green]
>> "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
>> news:lZ_xd.689446$mD.299515@attbi_s02...[color=darkred]
>>>
>>> "McKirahan" <News@McKirahan.com> wrote in message
>>> news:7C_xd.268087$HA.48168@attbi_s01...
>>> > "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
>>> > news:Sj_xd.302269$R05.120039@attbi_s53...
>>> >> Hello,
>>> >>
>>> >> I need to look through the text on a page and replace certain words[/color]
>> with[color=darkred]
>>> > an
>>> >> image or other word
>>> >>
>>> >> something like:
>>> >>
>>> >> read document
>>> >> find all instances of the word "blah"
>>> >> change all instances of the word "blah" to <img
>>> >> src="MyPicture.jpg"
>>>
>>> >>
>>> >>
>>> >> (jeez, I don't get why computers cant just read english!<G>)
>>> >> and, no, I cant just change the page with 'find/replace' in notepad.
>>> >>
>>> >> --
>>> >> thanks,
>>> >> juglesh B>{)}
>>> >>
>>> >>
>>> >
>>> > It is not clear -- who's page?
>>>
>>> well, my .php is being included in their page(which they made for me) .[/color]
>> my[color=darkred]
>>> .php page contains the head and the body tag, so i can put in a[/color]
>> javascript,[color=darkred]
>>> and call it from body onload. basically, when it gets to your browser,[/color]
>> you[color=darkred]
>>> see my head content, and most of the body is theirs.
>>>
>>> >
>>> > For any given URL do you want to change "certain words" and display it[/color]
>> or[color=darkred]
>>> > what?
>>>
>>> so, not any given url, just on the pages where my head content is used.
>>>
>>> their page is:
>>> <?php include 'http://www.mydomain.com/head.php'; ?>
>>> <p> Welcome. Content is here. blah and blah</p>
>>> </body>
>>>
>>> my head.php is
>>> <head>
>>> <my javascript>
>>> </head>
>>> <body>
>>>
>>> i want my js to change all instances of 'blah' to <img
>>> src="MyPicture.jpg"
>>>
>>>
>>> which would change the output to:
>>> Welcome. Content is here. <the image> and <the image>
>>>[/color]
>>
>> Will this work? Watch for word-wrap.
>>
>> <head>
>> <script type="text/javascript">
>> function MyHead() {
>> var find = "blah and blah";
>> var repl = "<img src='MyPicture.jpg'>";
>> var page = document.body.innerHTML;
>> while (page.indexOf(find) >= 0) {
>> var i = page.indexOf(find);
>> var j = find.length;
>> page = page.substr(0,i) + repl + page.substr(i+j);
>> document.body.innerHTML = page;
>> }
>> }
>> </script>
>> </head>
>> <body onload="MyHead()">[/color]
>
> almost works! if i change repl to a different word, it will work (it will
> display another word), but if i use that img tag, nothing changes.[/color]

ahh, think i got it! i had to change my quotes in the img tag to single
quotes.

is find a special variable? if i wanted to find and replace some other
words, how could i do that without making a bunch more functions?

thank you very much.



RobB
Guest
 
Posts: n/a
#8: Jul 23 '05

re: replace instances of text on page with image or other text?



juglesh wrote:[color=blue]
> Hello,
>
> I need to look through the text on a page and replace certain words[/color]
with an[color=blue]
> image or other word
>
> something like:
>
> read document
> find all instances of the word "blah"
> change all instances of the word "blah" to <img[/color]
src="MyPicture.jpg" >[color=blue]
>
>
> (jeez, I don't get why computers cant just read english!<G>)
> and, no, I cant just change the page with 'find/replace' in notepad.
>
> --
> thanks,
> juglesh B>{)}[/color]
Maybe...

http://www.alistapart.com/articles/dynatext/

McKirahan
Guest
 
Posts: n/a
#9: Jul 23 '05

re: replace instances of text on page with image or other text?


"juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
news:cP1yd.303759$R05.64258@attbi_s53...

[snip]
[color=blue]
>
> ahh, think i got it! i had to change my quotes in the img tag to single
> quotes.
>
> is find a special variable? if i wanted to find and replace some other
> words, how could i do that without making a bunch more functions?
>
> thank you very much.[/color]


"var find" and "var repl" are just variable names.


Martin Bialasinski
Guest
 
Posts: n/a
#10: Jul 23 '05

re: replace instances of text on page with image or other text?


"juglesh" <juglesh@nospamRadioKDUG.com> wrote:
[color=blue]
> read document
> find all instances of the word "blah"
> change all instances of the word "blah" to <img src="MyPicture.jpg" >
>
> (jeez, I don't get why computers cant just read english!<G>)[/color]

Well, it nearly looks like Applescript, which has a syntax like

tell application "Finder" to set x to entire contents of folder
"Macintosh HD:System Folder"

:-)
juglesh
Guest
 
Posts: n/a
#11: Jul 23 '05

re: replace instances of text on page with image or other text?



"McKirahan" <News@McKirahan.com> wrote in message
news:Xr4yd.304544$R05.275437@attbi_s53...[color=blue]
> "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
> news:cP1yd.303759$R05.64258@attbi_s53...
>
> [snip]
>[color=green]
>>
>> ahh, think I got it! I had to change my quotes in the img tag to single
>> quotes.
>>
>> is find a special variable? if I wanted to find and replace some other
>> words, how could I do that without making a bunch more functions?
>>
>> thank you very much.[/color]
>
>
> "var find" and "var repl" are just variable names.[/color]

yeah, ok, dreamweaver colors 'find' a different color, so I was thinking
that some special variable name or something.

well, I got it to work replacing 2 different words with two different
images, but only by duplicating the function (and adding a 2 to all the
variables). but I cant seem to do multiple replaces within the same
function. It seems like if I want to do 4 or 5 of these, its gonna get a
bit too much code if it could be done in one function.

thanks,



McKirahan
Guest
 
Posts: n/a
#12: Jul 23 '05

re: replace instances of text on page with image or other text?


"juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
news:F6Eyd.5255$k25.1585@attbi_s53...

[snip]
[color=blue][color=green]
> > "var find" and "var repl" are just variable names.[/color]
>
> yeah, ok, dreamweaver colors 'find' a different color, so I was thinking
> that some special variable name or something.
>
> well, I got it to work replacing 2 different words with two different
> images, but only by duplicating the function (and adding a 2 to all the
> variables). but I cant seem to do multiple replaces within the same
> function. It seems like if I want to do 4 or 5 of these, its gonna get a
> bit too much code if it could be done in one function.
>
> thanks,[/color]

"find" is probably a reserved word in some language.

The version below handles multiple find/repl:

<head>
<script type="text/javascript">
function MyHead() {
var list = new Array();
list[0] = "blah1^<img src='MyPicture1.jpg'>";
list[1] = "blah2^<img src='MyPicture2.jpg'>";
list[2] = "blah3^<img src='MyPicture3.jpg'>";
var j, k, find, item, page, repl;
for (var i=0; i<list.length; i++) {
item = list[i].split("^");
find = item[0];
repl = item[1];
page = document.body.innerHTML;
while (page.indexOf(find) >= 0) {
var j = page.indexOf(find);
var k = find.length;
page = page.substr(0,j) + repl + page.substr(j+k);
document.body.innerHTML = page;
}
}
}
</script>
</head>
<body onload="MyHead()">


juglesh
Guest
 
Posts: n/a
#13: Jul 23 '05

re: replace instances of text on page with image or other text?



"McKirahan" <News@McKirahan.com> wrote[color=blue]
>
> The version below handles multiple find/repl:
>
> <head>
> <script type="text/javascript">
> function MyHead() {
> var list = new Array();
> list[0] = "blah1^<img src='MyPicture1.jpg'>";
> list[1] = "blah2^<img src='MyPicture2.jpg'>";
> list[2] = "blah3^<img src='MyPicture3.jpg'>";
> var j, k, find, item, page, repl;
> for (var i=0; i<list.length; i++) {
> item = list[i].split("^");
> find = item[0];
> repl = item[1];
> page = document.body.innerHTML;
> while (page.indexOf(find) >= 0) {
> var j = page.indexOf(find);
> var k = find.length;
> page = page.substr(0,j) + repl + page.substr(j+k);
> document.body.innerHTML = page;
> }
> }
> }
> </script>
> </head>
> <body onload="MyHead()">[/color]

thank you very much, this is perfect!!! One hint, don't try naming your
images the same as the word you are trying to replace<G> (blah1.gif-doesn't
work). throws IE into a tailspin.

seems to run fast enough on my puter(xp, 2.8ghz, 512ram), do you think it
would be a problem on a lesser machine? at what point, x number of replaced
words, or x number of instances of the words in the page, would it be a
problem? I ask cuz it seems to me that we are storing the
document.body.innerHTML a lot. seems a lot of stuff to have in memory and
be looking thru multiple times, but I don't really know.

--

juglesh


McKirahan
Guest
 
Posts: n/a
#14: Jul 23 '05

re: replace instances of text on page with image or other text?


"juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
news:Tf7zd.630520$D%.101935@attbi_s51...

[snip]
[color=blue]
> thank you very much, this is perfect!!! One hint, don't try naming your
> images the same as the word you are trying to replace<G>[/color]
(blah1.gif-doesn't[color=blue]
> work). throws IE into a tailspin.
>
> seems to run fast enough on my puter(xp, 2.8ghz, 512ram), do you think it
> would be a problem on a lesser machine? at what point, x number of[/color]
replaced[color=blue]
> words, or x number of instances of the words in the page, would it be a
> problem? I ask cuz it seems to me that we are storing the
> document.body.innerHTML a lot. seems a lot of stuff to have in memory[/color]
and[color=blue]
> be looking thru multiple times, but I don't really know.
>
> --
>
> juglesh
>[/color]
Then let's only use it once:

<head>
<script type="text/javascript">
function MyHead() {
var list = new Array();
list[0] = "blah1^<img src='MyPicture1.jpg'>";
list[1] = "blah2^<img src='MyPicture2.jpg'>";
list[2] = "blah3^<img src='MyPicture3.jpg'>";
var j, k, find, item, repl;
var page = document.body.innerHTML;
for (var i=0; i<list.length; i++) {
item = list[i].split("^");
find = item[0];
repl = item[1];
page = document.body.innerHTML;
while (page.indexOf(find) >= 0) {
var j = page.indexOf(find);
var k = find.length;
page = page.substr(0,j) + repl + page.substr(j+k);
}
}
document.body.innerHTML = page;
}
</script>
</head>
<body onload="MyHead()">

Also, I'd suggest that instead of using "blah" use a tag-like reference such
as [blah] si it will stand out more and can't be confused with an image
name.


juglesh
Guest
 
Posts: n/a
#15: Jul 23 '05

re: replace instances of text on page with image or other text?



"McKirahan" <News@McKirahan.com> wrote in message
news:0qCzd.19336$k25.3159@attbi_s53...[color=blue]
> "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
> news:Tf7zd.630520$D%.101935@attbi_s51...
>
> [snip]
>[color=green]
>> thank you very much, this is perfect!!! One hint, don't try naming your
>> images the same as the word you are trying to replace<G>[/color]
> (blah1.gif-doesn't[color=green]
>> work). throws IE into a tailspin.
>>
>> seems to run fast enough on my puter(xp, 2.8ghz, 512ram), do you think it
>> would be a problem on a lesser machine? at what point, x number of[/color]
> replaced[color=green]
>> words, or x number of instances of the words in the page, would it be a
>> problem? I ask cuz it seems to me that we are storing the
>> document.body.innerHTML a lot. seems a lot of stuff to have in memory[/color]
> and[color=green]
>> be looking thru multiple times, but I don't really know.
>>
>> --
>>
>> juglesh
>>[/color]
> Then let's only use it once:
>
> <head>
> <script type="text/javascript">
> function MyHead() {
> var list = new Array();
> list[0] = "blah1^<img src='MyPicture1.jpg'>";
> list[1] = "blah2^<img src='MyPicture2.jpg'>";
> list[2] = "blah3^<img src='MyPicture3.jpg'>";
> var j, k, find, item, repl;
> var page = document.body.innerHTML;
> for (var i=0; i<list.length; i++) {
> item = list[i].split("^");
> find = item[0];
> repl = item[1];
> page = document.body.innerHTML;
> while (page.indexOf(find) >= 0) {
> var j = page.indexOf(find);
> var k = find.length;
> page = page.substr(0,j) + repl + page.substr(j+k);
> }
> }
> document.body.innerHTML = page;
> }
> </script>
> </head>
> <body onload="MyHead()">
>
> Also, I'd suggest that instead of using "blah" use a tag-like reference
> such
> as [blah] si it will stand out more and can't be confused with an image
> name.[/color]

this last one isnt working here. only the third item is replaced.

I sorta see where you're going, though. you only do the
document.body.innerHTML = page at the end, and not through every for/while
loop? as you may have guessed, I'm just a copy/paste javascripter, I get
most of it, and if I squint at the code for long enough while flipping
through javascript references online, I can grok a bit more.

afa [blah] , do you mean that I should have the text to be replaced in the
html changed to bracketed? because that's why I'm going through all this in
the first place, I cant change most of the body of the page (well, not
without js).

--
juglesh


McKirahan
Guest
 
Posts: n/a
#16: Jul 23 '05

re: replace instances of text on page with image or other text?


"juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
news:6LGzd.261461$V41.16042@attbi_s52...

[snip]

Remove the instance of
page = document.body.innerHTML;
just before the "while" statement.

I just meant that if you had control obver the page it would be nice to more
clearly identify replacable elements.

[color=blue]
> this last one isnt working here. only the third item is replaced.
>
> I sorta see where you're going, though. you only do the
> document.body.innerHTML = page at the end, and not through every for/while
> loop? as you may have guessed, I'm just a copy/paste javascripter, I get
> most of it, and if I squint at the code for long enough while flipping
> through javascript references online, I can grok a bit more.
>
> afa [blah] , do you mean that I should have the text to be replaced in the
> html changed to bracketed? because that's why I'm going through all this[/color]
in[color=blue]
> the first place, I cant change most of the body of the page (well, not
> without js).
>
> --
> juglesh
>
>[/color]


juglesh
Guest
 
Posts: n/a
#17: Jul 23 '05

re: replace instances of text on page with image or other text?



"McKirahan" <News@McKirahan.com> wrote in message
news:qOKzd.812226$8_6.276494@attbi_s04...[color=blue]
> "juglesh" <juglesh@nospamRadioKDUG.com> wrote in message
> news:6LGzd.261461$V41.16042@attbi_s52...
>
> [snip]
>
> Remove the instance of
> page = document.body.innerHTML;
> just before the "while" statement.[/color]

cool.
[color=blue]
> I just meant that if you had control obver the page it would be nice to
> more
> clearly identify replacable elements.[/color]

true, dat. I'm trying to get them to call my replaceElements.php so i can
just put some urls to my images where i need them.


Closed Thread


Similar JavaScript / Ajax / DHTML bytes