Connecting Tech Pros Worldwide Help | Site Map

strange...

 
LinkBack Thread Tools Search this Thread
  #1  
Old November 16th, 2006, 08:35 AM
josh
Guest
 
Posts: n/a
Default strange...

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!


  #2  
Old November 16th, 2006, 09:15 AM
shimmyshack
Guest
 
Posts: n/a
Default Re: strange...

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

  #3  
Old November 16th, 2006, 10:25 AM
VK
Guest
 
Posts: n/a
Default Re: strange...

shimmyshack wrote:
Quote:
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).

  #4  
Old November 16th, 2006, 12:05 PM
Julian Turner
Guest
 
Posts: n/a
Default Re: strange...


josh wrote:
Quote:
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:-
Quote:
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.
Quote:
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

  #5  
Old November 16th, 2006, 01:25 PM
Richard Cornford
Guest
 
Posts: n/a
Default Re: strange...

VK wrote:
Quote:
shimmyshack wrote:
Quote:
>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.
Quote:
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?
Quote:
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.


  #6  
Old November 16th, 2006, 06:45 PM
Randy Webb
Guest
 
Posts: n/a
Default Re: strange...

shimmyshack said the following on 11/16/2006 5:36 AM:
Quote:
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/
  #7  
Old November 16th, 2006, 07:15 PM
shimmyshack
Guest
 
Posts: n/a
Default Re: strange...

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?

  #8  
Old November 16th, 2006, 07:15 PM
Randy Webb
Guest
 
Posts: n/a
Default Re: strange...

shimmyshack said the following on 11/16/2006 3:32 PM:
Quote:
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/
 

Bookmarks

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Popular Articles

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over 220,989 network members.