By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,397 Members | 1,420 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,397 IT Pros & Developers. It's quick & easy.

strange...

P: n/a
Hi, if I have an array like this:

var a = new array(1,2,3);

then I make

txt.value = a.join("\n");

where txt is a textarea, in that textarea the string returned from join
is correctly formatted as
tre values separated by a line feed and so displayed ... but if I pass
the delimiter from a variable
then the texarea show also this delimiter...

var del = form.del.value; // here value entered is \n
txt.value = a.join(del);

in the textarea I'll have:
1\n2\n3
and not
1
2
3
Why?

P.S. If I pass a value like %0D and then make:
txt.value = unescape(a.join(del));
it works well!

Nov 16 '06 #1
Share this Question
Share on Google+
7 Replies


P: n/a
double quotes are magic "\n" is line brk. '\n' isnt.
try it in an alert as well, it works there, and you can do the same
with tabs \t and return characters \r

Nov 16 '06 #2

P: n/a
VK
shimmyshack wrote:
double quotes are magic "\n" is line brk. '\n' isnt.
Only in Perl - and here is JavaScript ;-) JavaScript doesn't care of
quotes type as long as they are matching each other.

To OP: do you have an URL to look at? (And remember that '\n' is
JavaScript is not exactly NL code, it's acting more as a
system-dependant constant like vbNewLine in VBA. This way one cane add
"\n" on Linux or on Windows and still get normal line breaks in
textarea).

Nov 16 '06 #3

P: n/a

josh wrote:
Hi, if I have an array like this:

var a = new array(1,2,3);

then I make

txt.value = a.join("\n");

where txt is a textarea, in that textarea the string returned from join
is correctly formatted as
tre values separated by a line feed and so displayed ... but if I pass
the delimiter from a variable
then the texarea show also this delimiter...

var del = form.del.value; // here value entered is \n
txt.value = a.join(del);

in the textarea I'll have:
1\n2\n3
and not
1
2
3
Why?

P.S. If I pass a value like %0D and then make:
txt.value = unescape(a.join(del));
it works well!
Hi

Your problem is here:-
var del = form.del.value; // here value entered is \n
txt.value = a.join(del);
in that you are confusing escaped characters in JavaScript "string
literals" in code, and text entered into a text area or input box.

If you use "\n" or '\n' in a string literal in JavaScript code, then
this is converted by the JavaScript parser into an 0A character.

If you type \n into a textarea or text box, your textarea or input box
does not recognise this as an escaped character, simply as "\" and "n"
literally. It is the equivalent of "\\n" in code.
P.S. If I pass a value like %0D and then make:
txt.value = unescape(a.join(del));
it works well!
This case is no different, but for one exception, you have added
"unescape", which does the work of converting %0D into an 0D character.

In your first example you need to do the same, i.e. write some code
which looks for and can convert a "\n" into an 0A character.

Regards

Julian Turner

Nov 16 '06 #4

P: n/a
VK wrote:
shimmyshack wrote:
>double quotes are magic "\n" is line brk. '\n' isnt.

Only in Perl - and here is JavaScript ;-) JavaScript doesn't care of
quotes type as long as they are matching each other.

To OP: do you have an URL to look at?
(And remember that '\n' is
JavaScript is not exactly NL code,
Maybe not, but it is LF (line feed), code point 000A (hex), always and
without exception.
it's acting more as a
system-dependant constant like vbNewLine in VBA.
Nonsense, and if that were the case why would javascript not provide an
escape sequence that was always code point 000A, or why would it provide
'\r' CR (carriage return, code point 000D) as an escape sequence?
This way one cane add "\n" on Linux or on Windows and
still get normal line breaks in textarea).
Any OS specific translation of a javascript string into a form
appropriate to a textarea on a particular OS happens at the point of
assigning the sting to the - value - property of the host object (as
when a javascript string is passed as an argument to a host-provided
function, and the function transforms its argument once called) and does
not make '\n' in javascript anything other than code point 000A, and
centrally not an OS dependent line terminator.

Richard.
Nov 16 '06 #5

P: n/a
shimmyshack said the following on 11/16/2006 5:36 AM:
double quotes are magic "\n" is line brk. '\n' isnt.
Nonsense.

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 16 '06 #6

P: n/a
yeah I was wrong about that, in javascript there is no such facility,
as there is in many other languages perl, php, etc...

- thanx 4 the constructive reply though randy - had a bad day?

Nov 16 '06 #7

P: n/a
shimmyshack said the following on 11/16/2006 3:32 PM:
yeah I was wrong about that, in javascript there is no such facility,
as there is in many other languages perl, php, etc...

- thanx 4 the constructive reply though randy - had a bad day?
Nah, just one of those days where I want to be bluntly honest with people :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Nov 16 '06 #8

This discussion thread is closed

Replies have been disabled for this discussion.