Click to See Complete Forum and Search --> : little help (really easy n quick)


beedee
12-16-2002, 08:42 PM
check this out

how can i make it so it replaces say an 's' with an 'sh'? the browser just crashes when i try it :(

cheers

Beach Bum
12-16-2002, 08:58 PM
check what out?

i think you forgot the link

beedee
12-16-2002, 09:14 PM
http://javascript.internet.com/forms/replace-characters.html

sorry

beedee
12-16-2002, 11:01 PM
bump! :D

beedee
12-16-2002, 11:13 PM
anyone? :(

khalidali63
12-16-2002, 11:32 PM
the code should work beedee,
I am sure there is something in your code which is wrong...instead of the link can you paste your code that does not work?

beedee
12-16-2002, 11:38 PM
HERES THE CODE (ILL EXPLAIN WHATS WRONG BELOW)

-------

<SCRIPT LANGUAGE="JavaScript">

<!-- This script and many more are available free online at -->
<!-- The JavaScript Source!! http://javascript.internet.com -->

<!-- Begin
function replaceChars(entry) {
out = "a"; // replace this
add = "z"; // with this
temp = "" + entry; // temporary holder

while (temp.indexOf(out)>-1) {
pos= temp.indexOf(out);
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}
document.subform.text.value = temp;
}
// End -->
</script>
</HEAD>

<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<center>
<form name="subform">

<input type=text name=text size=40 value=""><br>
<input type=button name=action value="Done!" onClick="replaceChars(document.subform.text.value);">

--------------

OK so i want to change it so it will change any "s" or "S" into "sh" or "Sh" its easy i know but i cant work out how to do it. It messes it up if you try and change a "s" into a "sh" because there is another "s". So i though you should change the "s" into (say) "zh" and then back into "sh" and the same for the caps version.....im a bit new to all this :(

beedee
12-17-2002, 12:04 AM
raptor i got an email saying you had replied? but i cant see the reply :(

Raptor
12-17-2002, 12:05 AM
I made an error, so quickly deleted it. Have another version in a few minutes.

beedee
12-17-2002, 12:06 AM
thank you! :D

khalidali63
12-17-2002, 12:14 AM
here you go beedee, the code below should get you going

:-)

<body>
<SCRIPT LANGUAGE="JavaScript">

function replaceChars(entry) {
out = "s"; // replace this
add = "h"; // with this
temp = "" ;
while (entry.indexOf(out)>-1) {
pos= entry.indexOf(out);
var t1 = entry.substring(0, pos) + out + add;
temp += t1;
entry = entry.substring(pos+1,entry.length);
}
document.subform.text.value = temp;
}
</script>
</head>
<center>
<form name="subform">

<input type=text name=text size=40 value=""><br>
<input type=button name=action value="Done!" onClick="replaceChars(document.subform.text.value);">
</body>

beedee
12-17-2002, 12:18 AM
thats exactly right :D (cap S) dont work...but it dosent matter.......thank you very much

beedee
12-17-2002, 12:20 AM
actually instead of say replacing 'sun' with 'shun' it replaces it with 'sh' any thoughts........:( :)

khalidali63
12-17-2002, 12:20 AM
You are very welcome,
and good night

_:-)

Khalid

Raptor
12-17-2002, 12:24 AM
You might find this works better for your needs.

var frompos = 0;
while( temp.indexOf( out, frompos ) > -1 )
{
pos= temp.indexOf( out, frompos );
frompos = pos + add.length;
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}

beedee
12-17-2002, 12:25 AM
where do i put this?

Raptor
12-17-2002, 12:27 AM
Slightly optimized and the full code for less confusion:


<html>
<script language=javascript>
function replaceChars(entry) {
out = "s"; // replace this
add = "Sh"; // with this
temp = "" + entry; // temporary holder

var frompos = 0;
while( ( pos = temp.indexOf( out, frompos ) ) > -1 )
{
frompos = pos + add.length;
temp = "" + (temp.substring(0, pos) + add +
temp.substring((pos + out.length), temp.length));
}
document.subform.text.value = temp;
}
// End -->
</script>
</HEAD>

<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#FFFFFF" text="#000000">
<center>
<form name="subform">

<input type=text name=text size=40 value=""><br>
<input type=button name=action value="Done!" onClick="replaceChars(document.subform.text.value);">
</html>

beedee
12-17-2002, 12:31 AM
thank you that works fine :D