Click to See Complete Forum and Search --> : replace text


doug777
01-12-2004, 01:08 AM
I want to change currency symbols in the http text of a page to a value previously set in a cookie. Is there a simple way to change for example USD to GBP in some text.

Could I surround the text I may need to change with say a span tag with an ID e.g. some text <span ID="currsym">USD</span> some more text.

and then use body onLoad to trigger a javascript function that uses getELementByID to get the span tag? And if so how is the content of the tag addressed?

Or is there another way to do this.

Doug

Pittimann
01-12-2004, 01:18 AM
Hi!

What you want is absolutely possible. You could use:
document.getElementById('currsym').innerText="$" (or pounds) in your function.

To avoid problems with some browsers you could as well use document.write to display the symbol you need...

Cheers - Pit

doug777
01-12-2004, 02:07 AM
Thanks for reply.

I don't want to use document.write because in the real page there could be many of these currency symbols scattered all over the place. I think it will be easier to call them currsymn where n is a number I can loop through.

But why then doesn't the following work:

<html>
<head>

<script language="JavaScript">
<!--

function NewText() {
document.getEelementByID('currsym').innerText="no";
}

//-->
</script>

</head>

<body onLoad="javascript:NewText()">
<p>This is a test <span id="currsym">yes</span>.</p>
</body>
</html>

Doug

Pittimann
01-12-2004, 03:00 AM
Hi!

Your code says "getEelementByID" instead of "getElementByID".

The other thing is: you shouldn't assign identical id's to different spans to get the loop thing work. If you call them currsym1, currsym2, currsym3 etc., you can achieve what you want.

Btw: even if you have many symbols scattered all over the place, you could use document.write (in this case you would have more browser compatibility as I said before)...

Cheers - Pit

Kor
01-12-2004, 03:11 AM
syntax errors

Try:

document.getElementById('currsym').innerHTML="no";


call them currsymn where n is a number I can loop through.


You may use another object getElementsByTagName('span') and loop through without using ID or name (which might be boring if a lot of objects are to be change on page). And there is no use to write javascript: when onload = "javascript:NewText()"


<html>
<head>
<script language="JavaScript">
<!--
function NewText() {
for (i=0;i<document.getElementsByTagName('span').length;i++){
document.getElementsByTagName('span')[i].innerHTML="no";
}
}
//-->
</script>

</head>

<body onLoad="NewText()">
<p>This is a test <span>yes</span>.</p>
<p>This is a test <span>yes</span>.</p>
<p>This is a test <span>yes</span>.</p>
</body>
</html>

doug777
01-12-2004, 03:12 AM
Corrected my typo, but still doesn't work.

Presumably to use document.write I have to build up a string of the entire page at least between the first and last piece of text I want to change. This could include tables and forms etc.

In fact it's not a big deal for those people with older browsers; they just won't be able to view the prices in other currencies.

Is it easier to use document.write, or persevere and get working this method which feels simpler to me?

Many thanks for all your help.
Doug

Pittimann
01-12-2004, 03:18 AM
Hi!

Most likely you will be entirely satisfied with the code, Kor provided.

An example for the doc.write version:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>Untitled</title>
<script language="JavaScript" type="text/javascript">
<!--
var currVar="£"
//in your example get the value from your cookie...
//-->
</script>
</head>
<body>
For that, you will have to pay me <script language="JavaScript" type="text/javascript">
<!--
document.write(currVar);
//-->
</script>1,235.20. If you buy three items, I would only charge you <script language="JavaScript" type="text/javascript">
<!--
document.write(currVar);
//-->
</script>3,500.00 (and so on...)
</body>
</html>

Cheers - Pit

doug777
01-12-2004, 03:32 AM
Pit,

Many thanks, yes I can see that document.write using this method is easier than I thought.


Kor,

I still can't make this work. Even a simplified version without the loop still outputs yes and not no.

On pages where I don't otherwise use span, this system is simpler. Thanks.

But what have I got wrong. Here is my 'simple' code.

<html>
<head>

<script language="JavaScript">
<!--

function NewText() {
document.getElementsByTagName('span').innerHTML="no";
}

//-->
</script>

</head>

<body onLoad="NewText()">
<p>This is a test <span>yes</span>.</p>
</body>
</html>

Doug

Pittimann
01-12-2004, 03:43 AM
Hi!

I just saw, that Kor is offline...

You didn't put Kor's loop into your code (please check out the function he provided again)!

Cheers - Pit

doug777
01-12-2004, 03:53 AM
Okay thanks very much. I have it working now.

Many, many thanks.

Doug

Kor
01-12-2004, 03:54 AM
my code works ok, i verified it...

Further more, here's a more comprehensive and sophisticated code to replace a certain word on the page, without using any separator... Could be useful, I presume you can just take the old page and simply insert the code withoud any other changes in HTML code. Here is:


<html>
<head>
<script language="JavaScript">
<!--
function NewText() {
var text = document.body.innerHTML;
var s = text.split(' ');
ss='';
for (i=0;i<s.length;i++){
if(s[i] == 'yes'){
s[i] = 'no';
}
ss = ss+s[i]+' ';
}
document.body.innerHTML= ss;
}

//-->
</script>

</head>

<body onLoad="NewText()">
<p>This is a test yes </p>
<p>This is a test yes </p>
<p>This is a test yes </p>
</body>
</html>


Changes "yes" with "no" in all the page text

Kor
01-12-2004, 04:03 AM
... but If the text is too long I presume that the code will work very slow, as there might be a long incremental loop...

I reckon I wrote it only to see if there is a way to replace something in BODY without rewritting the BODY... An "academic" code, just for fun... :D

So I think that the first code, with getElementsByTagName, it will suit to you better

doug777
01-12-2004, 04:17 AM
Actually it doesn't matter if the objects gradually change to the new currency one after another as I doubt anyone could read the text faster than that.

I will try the different options and see which one gives the best result.

Again, many thanks for your help.

Doug

Pavka
01-12-2004, 11:21 AM
Hi Kor,
Thanks for your code to replace text in an html document. I found it very useful. When I tested it, however, I found that it works fine in IE, NS, and Mozilla, but not in Opera. Have you any idea why this might be?

Many thanks in advance for any help you can give me with this.

Lilli

Kor
01-12-2004, 12:06 PM
Well, you we, in Europe we don't use Opera almost at all... I just used some objects/methods W3 says are Ok for most of the new browsers.... In theory, those methods should be recognize by Opera too... what version you were tested with?

Pavka
01-13-2004, 04:15 AM
Hi Kor

Thanks for your quick reply. I was testing with Opera 6.01. The users of the site I manage do use Opera in fact.

I liked your "more sophisticated" code.

But let me tell you what the actual problem is. All I need to do is replace part of an email address via JavaScript in order to shield that person from spam. For example, I want to replace "xxx" in the user ID of xxx@hotmail.com with the proper user ID. Each user ID would probably appear only once in a given html page (although the loop would still be handy), but there could be several IDs to replace per page.

What are your thoughts on this?

Many thanks again. I appreciate your help.
Lilli

Kor
01-13-2004, 04:40 AM
All I need to do is replace part of an email address via JavaScript in order to shield that person from spam


Much better is to use a server-side application (php, asp, etc) to prevent the spam. Javascript is an "open" language, you can not hide anything from a medium javascript skilled user...

Anyway, yes, a relatively secured "shield" as you said may be done using an external JS file to replace the way you want.

Kor
01-13-2004, 05:21 AM
You need somethink like:


<html>
<head>
<script>
function mail(){
var xxx = new Array(3)
xxx[0] = 'john';
xxx[1] = 'mary';
xxx[2] = 'sarah';
for (i=0;i<document.getElementsByTagName('div').length;i++){
document.getElementsByTagName('div')[i].innerHTML = '<a href="mailto:'+xxx[i]+'@hotmail.com">'+xxx[i]+'@hotmail.com</a>';
}
}
</script>
</head>

<body onload="mail()">
<div></div>
<br>
<div></div>
<br>
<div></div>
</body>
</html>


or?:


<html>
<head>
<script>
function mail(p){
var xxx = new Array(3)
xxx[0] = 'john';
xxx[1] = 'mary';
xxx[2] = 'sarah';
document.getElementsByTagName('div')[p].innerHTML = '<a href="mailto:'+xxx[p]+'@hotmail.com">'+xxx[p]+'@hotmail.com</a>';
}
</script>
</head>

<body>
<div><a href="#" onclick="mail(0)">xxx@hotmail.com</a></div>
<br>
<div><a href="#" onclick="mail(1)">xxx@hotmail.com</a></div>
<br>
<div><a href="#" onclick="mail(2)">xxx@hotmail.com</a></div>
</body>
</html>