ijyoung
02-23-2006, 02:31 PM
This is probably an old chestnut but cannot find anything on net.
I have a script to format copy in a textarea which allows line breaks, etc to be translated into html when "onchange" activated. Works fine in Opera and IE but of course doesn't work in FF.
Code as follows:
function replace(string,text,by) {
// Replaces text with by in string
var strLength = string.length, txtLength = text.length;
if ((strLength == 0) || (txtLength == 0)) return string;
var i = string.indexOf(text);
if ((!i) && (text != string.substring(0,txtLength))) return string;
if (i == -1) return string;
var newstr = string.substring(0,i) + by;
if (i+txtLength < strLength)
newstr += replace(string.substring(i+txtLength,strLength),text,by);
return newstr;
}
function replace1(string) {
// Calls replace() many times to step through all the text changes we need
var a = "\r";
var b = "<br />";
var replaced=replace(string,a,b);
return replaced;
var a = "\n";
var b = "<br />";
var replaced=replace(string,a,b);
return replaced;
var a = "•";
var b = "·";
var replaced=replace(string,a,b);
var a = "*";
var b = "·";
var replaced=replace(string,a,b);
var a = "&";
var b = "&";
var replaced=replace(string,a,b);
var a = "-";
var b = "-";
var replaced=replace(string,a,b);
var a = "-";
var b = "-";
var replaced=replace(string,a,b);
var a = "§";
var b = "·";
var replaced=replace(string,a,b);
var a = "£";
var b = "£";
var replaced=replace(string,a,b);
}
textarea as follows:
<textarea rows="4" name="description" cols="40" onchange="description1=this.value;description1=replace1(description1);description.value=description1">
Anyone help?
Cheers
Ian
I have a script to format copy in a textarea which allows line breaks, etc to be translated into html when "onchange" activated. Works fine in Opera and IE but of course doesn't work in FF.
Code as follows:
function replace(string,text,by) {
// Replaces text with by in string
var strLength = string.length, txtLength = text.length;
if ((strLength == 0) || (txtLength == 0)) return string;
var i = string.indexOf(text);
if ((!i) && (text != string.substring(0,txtLength))) return string;
if (i == -1) return string;
var newstr = string.substring(0,i) + by;
if (i+txtLength < strLength)
newstr += replace(string.substring(i+txtLength,strLength),text,by);
return newstr;
}
function replace1(string) {
// Calls replace() many times to step through all the text changes we need
var a = "\r";
var b = "<br />";
var replaced=replace(string,a,b);
return replaced;
var a = "\n";
var b = "<br />";
var replaced=replace(string,a,b);
return replaced;
var a = "•";
var b = "·";
var replaced=replace(string,a,b);
var a = "*";
var b = "·";
var replaced=replace(string,a,b);
var a = "&";
var b = "&";
var replaced=replace(string,a,b);
var a = "-";
var b = "-";
var replaced=replace(string,a,b);
var a = "-";
var b = "-";
var replaced=replace(string,a,b);
var a = "§";
var b = "·";
var replaced=replace(string,a,b);
var a = "£";
var b = "£";
var replaced=replace(string,a,b);
}
textarea as follows:
<textarea rows="4" name="description" cols="40" onchange="description1=this.value;description1=replace1(description1);description.value=description1">
Anyone help?
Cheers
Ian