JRS: In article <nt******************@newsfe4-win.ntli.net>, dated Sat,
26 Aug 2006 21:57:39 remote, seen in news:comp.lang.javascript, Phil
Endecott <sp*******************@chezphil.orgposted :
>Dr John Stockton wrote:
>Phil Endecott posted :
>>I have some Javascript code that reads and sometimes sets the content of
a textarea. I want this to be reasonably browser and platform
independent. My question is, what characters should I expect to find at
the end of a line? I suspect that I need to cope with either "\n" or
"\r\n"; can someone confirm?
That may not mean what you think it means.
You mean to ask whether you need to cope with both LF and CR LF, where
LF is ASCII 10 & CR is ASCII 13, respectively Unicode \u000A \u000D .
Note that you *might* also find just CR.
In Javascript string, \n means newline, whatever that is locally, LF or
CR LF or CR - so splitting on \n should always be right.
Oh dear, yet another complication.
So on Windows, a string containing "\n" is actually two characters? Try
this:
var s = "\n";
alert(s.length);
I get 1.
We need to discriminate better in discussion between internal and
external representations of newline.
I have, in js-quick.htm, a textarea F.Code for execution.
Enter just "F.Code.value.length" and execute : I get result 19 ; add a
newline with the Enter key and execute : 21 ; another : 23.
Enter just "alert(F.Code.value.<ENTER>split(""))" and execute : result
shows two apparent newlines for the <ENTERkeystroke.
Enter just "alert(F.Code.value.<ENTER>split("\n"))" and execute : result
includes one apparent newline.
ISTM that the only really safe rule is that \n in a string outputs as
newline (probably always) and otherwise anything may happen!
To read a set of lines into an array, disregarding blank lines, I've
used return Ctrl.value.match(/([^\r\n]+)/g)
--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 MIME. ©
Web <URL:http://www.merlyn.demon.co.uk/- FAQqish topics, acronyms & links;
Astro stuff via astron-1.htm, gravity0.htm ; quotings.htm, pascal.htm, etc.
No Encoding. Quotes before replies. Snip well. Write clearly. Don't Mail News.