PDA

View Full Version : Parsing Lines with JavaScript Problem


gopat
03-14-2003, 03:30 AM
Hi,
I am trying to create a JavaScript program that will allow a
user to paste/type text into a text area box. When the user
clicks on a button the JavaScript code will store each line
into an array to allow specific logic to alter the text and
display the results in the output text area box.

The sample html/JavaScript below will give an idea of my program.
When you put text with some blank lines in the top box the output text
will show commas. This is bad.

Does any one have a solution to allow the program to copy text
without the commas that result from blank lines??
Thanks


<HTML>
<HEAD>
<TITLE>HowPage Validation & Document Creator</TITLE>

<SCRIPT LANGUAGE=JavaScript>
function testFunction(){
get_text(document.form1.text_area1, document.form1.text_area2)
}

//This is the main function that puts the input text into an array,
//then the other functions are called from here.
function get_text(text_input, text_output) {
var all_lines = new Array();
var tmp_lines = new Array();

//var all_lines = text_input.value.split('\n'); //Bad: Inserts commas
var all_lines = text_input.value.split('\n\r'); //Works for Linux, Not
windows...


//Add a new line character at the end of each line.
//Proccess each line, then send each task to a function:
for (line_nbr in all_lines)
{
// alert('line_nbr is ' + line_nbr);
// alert('all_lines[line_nbr] ' + all_lines[line_nbr]);
if (line_nbr < all_lines.length)
{
//This stores the above array to a new array with a line break.
//It is broken becuase the first character of each BLANK line
is a comma. ;-(

tmp_lines[line_nbr] = all_lines[line_nbr] + '\n';

}
// alert('tmp_lines[line_nbr] is ' + tmp_lines[line_nbr]);

}

text_output.value = tmp_lines;
}
// End of Main function###############################

</SCRIPT></HEAD>

<BODY>
<TABLE CELLSPACING=0 CELLSPACING="0" WIDTH="550" align="center"
BGCOLOR="white" BORDER="0" >


<!--- BEGIN USER INPUT/OUTPUT FORM SECTION: -->
<FORM NAME="form1">

<H2>JavaScript Parse Line Code Test</H2>


Type your input text in the box below, include some blank lines:
<P>

<!--- User TextArea input box: User pastes contents of text file here.-->
<TEXTAREA NAME="text_area1" ROWS="10" COLS="75" WRAP=VIRTUAL>
</TEXTAREA>
<P>

After clicking the left button, the above text should appear exactly the
same below.
<P>

<!--- Create Out Put Page Button: -->
<BR>
<INPUT TYPE="button" VALUE="Create OutPut Text"
ONCLICK="testFunction()"
>
<!--- Reset User TextArea input box: -->
<INPUT TYPE="reset" VALUE="Reset This Page">

<!--- Output HTML code to this TextArea box: select text button on top -->
<input type=button value="Highlight All Text"
onClick="javascript:document.form1.text_area2.focus();
document.form1.text_area2.select();">&nbsp;
<TEXTAREA NAME="text_area2" ROWS="10" COLS="90" WRAP=VIRTUAL>
</TEXTAREA>
<BR>
</FORM>
</BODY>
</HTML>

Roy Sinclair
03-14-2003, 06:59 PM
text_output.value = all_lines.join("\n\r");

Why recreate the wheel?

gopat
03-15-2003, 12:01 AM
Thanks! Super Simple!!!!

You have just made my day......