Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > replace instances of text on page with image or other text?

Reply
Thread Tools

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

 
 
juglesh
Guest
Posts: n/a
 
      12-23-2004

"McKirahan" <(E-Mail Removed)> wrote in message
news:Xr4yd.304544$R05.275437@attbi_s53...
> "juglesh" <(E-Mail Removed)> wrote in message
> news:cP1yd.303759$R05.64258@attbi_s53...
>
> [snip]
>
>>
>> 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.

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


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,



 
Reply With Quote
 
 
 
 
McKirahan
Guest
Posts: n/a
 
      12-23-2004
"juglesh" <(E-Mail Removed)> wrote in message
news:F6Eyd.5255$k25.1585@attbi_s53...

[snip]

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

>
> 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,


"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()">


 
Reply With Quote
 
 
 
 
juglesh
Guest
Posts: n/a
 
      12-25-2004

"McKirahan" <(E-Mail Removed)> wrote
>
> 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()">


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


 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      12-26-2004
"juglesh" <(E-Mail Removed)> wrote in message
news:Tf7zd.630520$D%.101935@attbi_s51...

[snip]

> 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
>

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.


 
Reply With Quote
 
juglesh
Guest
Posts: n/a
 
      12-26-2004

"McKirahan" <(E-Mail Removed)> wrote in message
news:0qCzd.19336$k25.3159@attbi_s53...
> "juglesh" <(E-Mail Removed)> wrote in message
> news:Tf7zd.630520$D%.101935@attbi_s51...
>
> [snip]
>
>> 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
>>

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


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


 
Reply With Quote
 
McKirahan
Guest
Posts: n/a
 
      12-27-2004
"juglesh" <(E-Mail Removed)> 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.


> 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
>
>



 
Reply With Quote
 
juglesh
Guest
Posts: n/a
 
      12-27-2004

"McKirahan" <(E-Mail Removed)> wrote in message
news:qOKzd.812226$8_6.276494@attbi_s04...
> "juglesh" <(E-Mail Removed)> wrote in message
> news:6LGzd.261461$V41.16042@attbi_s52...
>
> [snip]
>
> Remove the instance of
> page = document.body.innerHTML;
> just before the "while" statement.


cool.

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


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.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
dicts,instances,containers, slotted instances, et cetera. ocschwar@gmail.com Python 8 01-29-2009 09:52 AM
show me how to replace all instances of word in textbox alxasa@gmail.com Javascript 17 12-09-2006 03:55 PM
replace text in IFRAME using innerHTML.replace(...) possible? mscir Javascript 0 10-11-2005 11:21 PM
list of class instances within a list of a class instances John Wohlbier Python 2 02-22-2004 08:41 AM
Replace every n instances of a string Tom Cross Python 1 08-15-2003 11:05 PM



Advertisments