PDA

View Full Version : JavaScript handling newline characters


dank
01-10-2001, 04:04 PM
I have a script that uses onClick to send different values to a form field through JavaScript.[nbsp][nbsp]One problem I've run into is when using newline characters (\n) in the passed value (for representing blank lines in the textarea).[nbsp][nbsp]Doing so creates blank lines in the actual html source code, which is what the JavaScript sees client-side, resulting in something that apparently cannot be interpreted.

I can sort of get around this by placing
tags in the passed value instead of \n, then doing an ereg_replace to switch
back to \n once the form is submitted, but that simply places
's into the form textarea, whereas blank lines would be much preferred.

So, to make a long story short, does anyone know of a way to make JavaScript understand everything between single quotes, regardless of whether or not it is on the same line?

edit:[nbsp][nbsp]The next best thing I can think of is to refresh the page with onClick, passing a variable back in for which template to load (the JavaScript would only see the variable, not the actual template with newlines), but I'd rather not have to reload the page every time...

Thanks,
Dan
[This message has been edited by dank (edited 01-10-01@4:07 pm)]

dank
01-10-2001, 06:17 PM
Well, I figured out a decent solution, although it loses some of the flexibility I had hoped for.[nbsp][nbsp]Instead of passing a variable (php; so the page (i.e. the JavaScript) sees it as a string of text once loaded in the browser) to the JavaScript function through onClick, I set it up to pass a number corresponding to which radio button was selected.[nbsp][nbsp]Then, the former php variables are placed in the JavaScript function as array variables, of the form:

var template = new Array();
template[1] = "blah...";

The passed in number is received as "text", so the JavaScript passes the appropriate string back to the form's textarea as template[text].[nbsp][nbsp]Seems a bit loopy, but it works...

Unfortunately, working with variables in JavaScript seems much less flexible than in PHP, so I'm not sure how well things will work like reading a file into and/or appending multiple lines into a variable...

Better ideas?

Dan

Rich
01-10-2001, 10:30 PM
Text fields are usually filled in by the user typing in them and I *think* special characters (like \n) are encoded in the process. So, if you are going to populate the text field programmatically, you would need to encode the characters.

Perhaps if you told us more about these text boxes that are populated by your script based on a radio button being pressed?

Rich

dank
01-10-2001, 11:11 PM
Hi Rich,

Getting the newline (\n) characters in and out of the form field is not a problem, per se, it's getting them into a variable that will be passed through a JavaScript function and into the form field that is problematic.[nbsp][nbsp]Let's see if I can illustrate in a clear fashion...


<script language=&quot;JavaScript&quot;>
<!--
function setTemplate(text) {
[nbsp][nbsp][nbsp][nbsp] document.email_form.message.value = text;
}
//-->
</script>

<?php
$template[1] = &quot;string of text...&quot;;
$template[2] = &quot;string of text...&quot;;
$template[3] = &quot;string of text...\n\n&quot;;
$template[3] .= &quot;string of text, continued...&quot;;
?>

<form>
<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;1&quot; onClick=&quot;setTemplate('<?php echo($template[1]); ?>')&quot;> Template 1

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;2&quot; onClick=&quot;setTemplate('<?php echo($template[2]); ?>')&quot;> Template 2

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;3&quot; onClick=&quot;setTemplate('<?php echo($template[3]); ?>')&quot;> Template 3

</form>


That will only work if the \n newlines in $template[3] are replaced with
's.[nbsp][nbsp]The reason seems to be that once PHP processes the file and sends it to the browser, the \n characters have already been replaced by their html equivalent, resulting in something that looks like:

onClick=&quot;setTemplate('string

rest of string')&quot;

which kills the JavaScript.[nbsp][nbsp]:(

The solution I outlined in the second post is the following:


<script language=&quot;JavaScript&quot;>
<!--
function setTemplate(text) {
[nbsp][nbsp][nbsp][nbsp] var template = new Array();
[nbsp][nbsp][nbsp][nbsp] template[1] = &quot;string of text...&quot;;
[nbsp][nbsp][nbsp][nbsp] template[2] = &quot;string of text...&quot;;
[nbsp][nbsp][nbsp][nbsp] template[3] = &quot;string of text...\n\nstring of text continued...\n\n&quot;;
[nbsp][nbsp][nbsp][nbsp] document.email_form.message.value = template[text];
}
//-->
</script>

<form>
<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;1&quot; onClick=&quot;setTemplate('1')&quot;> Template 1

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;2&quot; onClick=&quot;setTemplate('2')&quot;> Template 2

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;3&quot; onClick=&quot;setTemplate('3')&quot;> Template 3

</form>


That apparently works because the JavaScript gets to handle the newline characters before the browser tries to interpret it (?).[nbsp][nbsp]It's just much more limited what I can do with the templates in that fashion...

(edited to simplify the JavaScript examples)

Dan
[This message has been edited by dank (edited 01-10-01@11:14 pm)]

rsh
01-12-2001, 11:01 AM
You'll need to escape the escape character that designates the new line -- have you tried this?


<?php
$template[3] = &quot;string of text...\\n\\nstring of text continued...\\n\\n&quot;;
?>

...

<input type=&quot;radio&quot; name=&quot;template&quot; value=&quot;3&quot; onClick=&quot;setTemplate('<?php echo($template[3]); ?>')&quot;> Template



[This message has been edited by rsh (edited 01-12-01@12:16 pm)]

dank
01-12-2001, 01:06 PM
That's a good suggestion, but it doesn't seem to work.[nbsp][nbsp]The double (escaped) slashes show up in the html source code, keep the variable's text string all on one line, get passed through to the JavaScript function, and are spit out in the form textarea as &quot;\n&quot; as text (the slash no longer escaped).[nbsp][nbsp]I'm not quite sure how that last part is happening...

I know JavaScript offers pattern matching regular expressions, but I haven't seen anything for matching and replacing.[nbsp][nbsp]Is there anything of that sort available?

I'm thinking I'll veer toward full-blown, parseable templates, and use the JS method for small add-ons.

Dan