Welcome to the MacNN Forums.

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

You are here: MacNN Forums > Software - Troubleshooting and Discussion > Developer Center > Javascript word replace?

Javascript word replace?
Thread Tools
moonmonkey
Professional Poster
Join Date: Jan 2001
Location: Australia
Status: Offline
Reply With Quote
May 12, 2004, 05:59 AM
 
I need a Javascript which will look on a field for a certain words (rude ones) and replace them with other ones.

Any ideas?

I can't run PHP or ASP on my host.

Thanks!
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
May 12, 2004, 10:01 AM
 
It's pretty easy actually. Put the code below in an HTML page and try it out. You could put the action on something different from a button onClick (form submit, for example) and get a similar result.

[php]
<html>
<script language="JavaScript">

var badWordArray = new Array("shazbat", "fizisk", "schmoo");

function cleanText(formelement)
{
var replacementString = "XXXXX";

for ( i = 0; i < badWordArray.length; i++ )
{
formelement.value = formelement.value.replace(
badWordArray[i],
replacementString);
}
}

</script>

<h4>Pretend shazbat and fizisk are words you want to replace</h4>

<form>
<textarea name="jstext" rows="10" cols="60">
This is a shazbat idea and I fizisk it.
</textarea>
<br/>
<input type="button"
name="click me"
value="Click Me"
onClick="cleanText(document.forms[0].jstext)">
</form>
</html>
[/php]

Note that the code is not PHP, I just used the PHP vB Code tags to get syntax highlighting.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
DUNSEL
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
May 12, 2004, 01:10 PM
 
What happens if the user blocks scripts? Can you run serverside javascript?
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
genevish
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
May 12, 2004, 01:17 PM
 
JavaScript is client side, not server side. You'd need some other server side scripting language, such as PHP (which you already said was out).

You could prevent the client from using your site if they didn't have JavaScript though...
Scott Genevish
scott AT genevish DOT org
     
Turias
Mac Elite
Join Date: Nov 2003
Location: Minnesota
Status: Offline
Reply With Quote
May 12, 2004, 01:22 PM
 
There is server-side JavaScipt.

I have never used it but I have heard that it isn't very popular (or very good).
     
DUNSEL
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
May 12, 2004, 01:32 PM
 
Originally posted by genevish:
JavaScript is client side, not server side. You'd need some other server side scripting language, such as PHP (which you already said was out).

You could prevent the client from using your site if they didn't have JavaScript though...
You most certainly can run JS serverside. Most any scripting language is capable of spitting out HTML as literal strings. I was merely asking him if his hosting would allow it, since it doesn't support PHP or ASP.
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
philzilla
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
May 12, 2004, 02:11 PM
 
we can't say shazbat any more?
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
DUNSEL
Forum Regular
Join Date: Feb 2004
Location: Manhattan
Status: Offline
Reply With Quote
May 12, 2004, 02:35 PM
 
Didn't realize that was a curse in your area. Figured anything was ok except "Everton".
I saw a woman with a sweatshirt that said "Guess", so I said, "Implants?"
     
philzilla
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
May 12, 2004, 02:47 PM
 
haha, with most people around here, you'd be right. i'm not most people though
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
genevish
Mac Enthusiast
Join Date: Jan 1999
Location: Marietta, GA, USA
Status: Offline
Reply With Quote
May 12, 2004, 03:36 PM
 
Oops. I never knew you could use JavaScript on the server side. You'd still need a parsing engine on the server however which would be no different than installing PHP on the server, so I'm guessing his host won't support it.
Scott Genevish
scott AT genevish DOT org
     
moonmonkey  (op)
Professional Poster
Join Date: Jan 2001
Location: Australia
Status: Offline
Reply With Quote
May 12, 2004, 07:21 PM
 
Originally posted by Arkham_c:
It's pretty easy actually. Put the code below in an HTML page and try it out. You could put the action on something different from a button onClick (form submit, for example) and get a similar result.

[php]
<html>
<script language="JavaScript">

var badWordArray = new Array("shazbat", "fizisk", "schmoo");

function cleanText(formelement)
{
var replacementString = "XXXXX";

for ( i = 0; i < badWordArray.length; i++ )
{
formelement.value = formelement.value.replace(
badWordArray[i],
replacementString);
}
}

</script>

<h4>Pretend shazbat and fizisk are words you want to replace</h4>

<form>
<textarea name="jstext" rows="10" cols="60">
This is a shazbat idea and I fizisk it.
</textarea>
<br/>
<input type="button"
name="click me"
value="Click Me"
onClick="cleanText(document.forms[0].jstext)">
</form>
</html>
[/php]

Note that the code is not PHP, I just used the PHP vB Code tags to get syntax highlighting.
Thats great thanks.

I actually want to replace particular words with different words, like every occurrence of "shazbat" is replaced with "butshandy".

could this script be modified to do this?

Thanks!
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
May 12, 2004, 08:25 PM
 
Originally posted by moonmonkey:
I actually want to replace particular words with different words, like every occurrence of "shazbat" is replaced with "butshandy".

could this script be modified to do this?
Yes, it's not tough. Arrays in JavaScript are pretty cool:

[php]
<html>
<script language="JavaScript">

var replaceArray = new Array();
replaceArray["shazbat"] = "butshandy";
replaceArray["fizisk"] = "pharlap";
replaceArray["schmoo"] = "zazpaz";

function cleanText(formelement)
{
for (item in replaceArray)
{
formelement.value = formelement.value.replace(
item, replaceArray[item]);
}
}

</script>

<h4>I actually want to replace particular words with different words, like every occurrence of "shazbat" is replaced with "butshandy".
</h4>

<form>
<textarea name="jstext" rows="10" cols="60">
This is a shazbat idea and I fizisk it. Just don't be a schmoo, ok?
</textarea>
<br/>
<input type="button" name="click me" value="Click Me"
onClick="cleanText(document.forms[0].jstext)">
</form>
</html>
[/php]
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
philzilla
Occasionally Useful
Join Date: Jun 2001
Location: Liverpool, UK
Status: Offline
Reply With Quote
May 12, 2004, 08:32 PM
 
you said zazpaz!

(okay, i'm going to bed now, before Mundy returns to this thread and berates me for not getting enough sleep again.)
"Have sharp knives. Be creative. Cook to music" ~ maxelson
     
thePurpleGiant
Mac Elite
Join Date: May 2001
Status: Offline
Reply With Quote
May 12, 2004, 08:38 PM
 
Originally posted by philzilla:
you said zazpaz!
My zazpaz ears!
     
moonmonkey  (op)
Professional Poster
Join Date: Jan 2001
Location: Australia
Status: Offline
Reply With Quote
May 13, 2004, 07:39 PM
 
Originally posted by Arkham_c:
Yes, it's not tough. Arrays in JavaScript are pretty cool:
Thanks for your help!
     
moonmonkey  (op)
Professional Poster
Join Date: Jan 2001
Location: Australia
Status: Offline
Reply With Quote
May 13, 2004, 11:09 PM
 
Originally posted by Arkham_c:
Yes, it's not tough. Arrays in JavaScript are pretty cool:

[php]
<html>
<script language="JavaScript">

var replaceArray = new Array();
replaceArray["shazbat"] = "butshandy";
replaceArray["fizisk"] = "pharlap";
replaceArray["schmoo"] = "zazpaz";

function cleanText(formelement)
{
for (item in replaceArray)
{
formelement.value = formelement.value.replace(
item, replaceArray[item]);
}
}

</script>

<h4>I actually want to replace particular words with different words, like every occurrence of "shazbat" is replaced with "butshandy".
</h4>

<form>
<textarea name="jstext" rows="10" cols="60">
This is a shazbat idea and I fizisk it. Just don't be a schmoo, ok?
</textarea>
<br/>
<input type="button" name="click me" value="Click Me"
onClick="cleanText(document.forms[0].jstext)">
</form>
</html>
[/php]


This doesn't seem to work on IE 6 on the PC.
Any ideas?
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
May 14, 2004, 02:29 PM
 
Originally posted by moonmonkey:
This doesn't seem to work on IE 6 on the PC.
Any ideas?
Yeah, it turns out that IE is a piece of crap.

This should work in IE:

[php]
<html>
<script language="JavaScript">

var replaceArray = new Array();
var keyArray = new Array();
addWord("shazbat", "butshandy");
addWord("fizisk", "pharlap");
addWord("schmoo", "zazpaz");

function addWord(oldword, newword)
{
replaceArray[oldword] = newword;
keyArray[keyArray.length] = oldword;
}

function cleanText(formelement)
{

for (i=0; i < keyArray.length; i++)
{
var item = keyArray[i];
formelement.value = formelement.value.replace(
item, replaceArray[item]);
}
}


</script>

<h4>I actually want to replace particular words with different words, like every occurrence of "shazbat" is replaced with "butshandy".
</h4>

<form>
<textarea name="jstext" rows="10" cols="60">
This is a shazbat idea and I fizisk it. Just don't be a schmoo, ok?
</textarea>
<br/>
<input type="button" name="click me" value="Click Me"
onClick="cleanText(document.forms[0].jstext)">
</form>
</html>
[/php]
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
Chris O'Brien
Grizzled Veteran
Join Date: Nov 2003
Location: Hebburn, UK
Status: Offline
Reply With Quote
May 14, 2004, 05:56 PM
 
Wow, two things that have come to my attention that I never knew (and I always presumed that I knew pretty much everything about JavaScript...)

1. The for...in object manipulation - i had asumed this was a weird thing in VB (for each something in sometheing else .. next something etc.... (i get paid to do VB, please don't hate me ). But it's right here, so I should've known better...

2. How bad IE was with arrays... I presumed it would be able to handle keywords in arrays (rather than just indexes...)

Anyhoo - thanks very much Arkham_c - despite me not being a part of this thread, I have learned something!
Just who are Britain? What do they? Who is them? And why?

Formerly Black Book
     
moonmonkey  (op)
Professional Poster
Join Date: Jan 2001
Location: Australia
Status: Offline
Reply With Quote
May 14, 2004, 11:51 PM
 
Thanks Arkham_c, you obviously are a very smart guy.

Cheers for you help.

Tom.
     
moonmonkey  (op)
Professional Poster
Join Date: Jan 2001
Location: Australia
Status: Offline
Reply With Quote
May 15, 2004, 11:16 PM
 
Damn.

That code doesn't work in IE 5 for Mac, also no luck in early versions of safari.
Any ideas?
     
Arkham_c
Mac Elite
Join Date: Dec 2001
Location: Atlanta, GA, USA
Status: Offline
Reply With Quote
May 17, 2004, 01:27 PM
 
Originally posted by moonmonkey:
That code doesn't work in IE 5 for Mac, also no luck in early versions of safari.
Any ideas?
I just tried it on IE 5.2 on OSX and it works fine for me there. I also tried Safari 1.2.1, Firefox 0.8 (Mozilla) for Mac and Windows, Camino 0.7, and IE6 for Windows, and all of them worked for me.

I have no way to test early versions of Safari, so I can't debug it. You could turn on the Safari debug menu and tell it to log JavaScript errors and post the result if you care enough to do so.

The last set of code I posted is very conservative and should work in any browser from Netscape 4/IE4 on up.
Mac Pro 2x 2.66 GHz Dual core, Apple TV 160GB, two Windows XP PCs
     
   
 
Forum Links
Forum 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
Top
Privacy Policy
All times are GMT -4. The time now is 11:12 AM.
All contents of these forums © 1995-2017 MacNN. All rights reserved.
Branding + Design: www.gesamtbild.com
vBulletin v.3.8.8 © 2000-2017, Jelsoft Enterprises Ltd.,