scottlyndon
September 4th, 2004, 10:47 AM
I am using a little bit of Javascript to Preview the contents of a textarea before the form is submitted. It opens a window and diaplsy the contents. This part works fine.
I need to convert carriage returns to html <br> line breaks. So if a user presses enter once in the textarea in the preview window it converts to one <br>, if there are two carriage returns it converts to two <br>'s.
The code currently converts any number of continuous carriage returns to just one line break.
For example, if the following is entered:
Line 1
Line 2
Line 3
It is currently showing in the preview window as follows:
Line1
Line2
Line3
Here is my code:
<script language="JavaScript">
function ShowPreview()
{
var PreviewWindow =
window.open("","Preview","width=400,height=250,scrollbars=yes,resizable=yes,status=0");
PreviewWindow.document.open();
PreviewWindow.document.writeln('<HTML><HEAD><TITLE>Preview Newsletter</TITLE></HEAD>');
PreviewWindow.document.writeln('<BODY>');
PreviewWindow.document.writeln('<font face="verdana" size="2"><b>Here what your newsletter body will look like:</b><br><br>');
PreviewWindow.document.writeln(document.SendForm.Body.value.replace(/\r/,"<br>"));
PreviewWindow.document.writeln('<br><br><a href="javascript:window.close()">Close Preview</a></font>');
PreviewWindow.document.writeln('</BODY></HTML>');
PreviewWindow.document.close();
}
</script>
I know it is something in the replace statement in this line:
PreviewWindow.document.writeln(document.SendForm.Body.value.replace(/\r/,"<br>"));
that is not correct. Anyone help me sort this? Ta.
I need to convert carriage returns to html <br> line breaks. So if a user presses enter once in the textarea in the preview window it converts to one <br>, if there are two carriage returns it converts to two <br>'s.
The code currently converts any number of continuous carriage returns to just one line break.
For example, if the following is entered:
Line 1
Line 2
Line 3
It is currently showing in the preview window as follows:
Line1
Line2
Line3
Here is my code:
<script language="JavaScript">
function ShowPreview()
{
var PreviewWindow =
window.open("","Preview","width=400,height=250,scrollbars=yes,resizable=yes,status=0");
PreviewWindow.document.open();
PreviewWindow.document.writeln('<HTML><HEAD><TITLE>Preview Newsletter</TITLE></HEAD>');
PreviewWindow.document.writeln('<BODY>');
PreviewWindow.document.writeln('<font face="verdana" size="2"><b>Here what your newsletter body will look like:</b><br><br>');
PreviewWindow.document.writeln(document.SendForm.Body.value.replace(/\r/,"<br>"));
PreviewWindow.document.writeln('<br><br><a href="javascript:window.close()">Close Preview</a></font>');
PreviewWindow.document.writeln('</BODY></HTML>');
PreviewWindow.document.close();
}
</script>
I know it is something in the replace statement in this line:
PreviewWindow.document.writeln(document.SendForm.Body.value.replace(/\r/,"<br>"));
that is not correct. Anyone help me sort this? Ta.