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

Character codes for newlines in textareas

P: n/a
Dear Experts,

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?

Setting the content is more of a challenge. I don't want to have nasty
browser detection to select what to use for newlines. Is there one
newline pattern that is safe for all browsers?

Many thanks for any suggestions.

Phil.
Aug 25 '06 #1
Share this Question
Share on Google+
6 Replies


P: n/a
*** Phil Endecott escribió/wrote (Fri, 25 Aug 2006 20:27:34 GMT):
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?
As far as I know, you can find:

\n in Unix/Linux
\r in MacOS 9 or older
\r\n in MacOS X and Windows

And several Unicode line endings nobody uses.

http://en.wikipedia.org/wiki/Line_endings

Setting the content is more of a challenge. I don't want to have nasty
browser detection to select what to use for newlines. Is there one
newline pattern that is safe for all browsers?
I've never had any problem just using \n in Windows or Linux, but I can't
tell about MacOS 9.
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Aug 25 '06 #2

P: n/a
Alvaro G. Vicario wrote:
*** Phil Endecott escribió/wrote (Fri, 25 Aug 2006 20:27:34 GMT):
>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?

As far as I know, you can find:

\n in Unix/Linux
\r in MacOS 9 or older
\r\n in MacOS X and Windows

And several Unicode line endings nobody uses.
Thanks. I have read that you may get \n in Mozilla, whatever OS it is
on. Best to split on any of them I think.
>Setting the content is more of a challenge. I don't want to have nasty
browser detection to select what to use for newlines. Is there one
newline pattern that is safe for all browsers?

I've never had any problem just using \n in Windows or Linux, but I can't
tell about MacOS 9.
Aug 25 '06 #3

P: n/a
*** Alvaro G. Vicario escribió/wrote (Fri, 25 Aug 2006 23:25:50 +0200):
\n in Unix/Linux
\r in MacOS 9 or older
\r\n in MacOS X and Windows
Typo (as usual in me):

\n in Unix/Linux and MacOS X
\r in MacOS 9 or older
\r\n in Windows
--
-+ http://alvaro.es - Álvaro G. Vicario - Burgos, Spain
++ Mi sitio sobre programación web: http://bits.demogracia.com
+- Mi web de humor con rayos UVA: http://www.demogracia.com
--
Aug 25 '06 #4

P: n/a
JRS: In article <W2******************@newsfe5-win.ntli.net>, dated Fri,
25 Aug 2006 20:27:34 remote, seen in news:comp.lang.javascript, Phil
Endecott <sp*******************@chezphil.orgposted :
>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.

Note that \n is a line separator; a file or string may or may not have
\n before the first line or after the last.

If I'm wrong, then <URL:http://www.merlyn.demon.co.uk/js-quick.htm>
buttons Pack & Indt may not work right on non-CRLF systems.

Indt now deals with many split-statement cases.

--
© John Stockton, Surrey, UK. ?@merlyn.demon.co.uk Turnpike v4.00 IE 4 ©
<URL:http://www.jibbering.com/faq/>? JL/RC: FAQ of news:comp.lang.javascript
<URL:http://www.merlyn.demon.co.uk/js-index.htmjscr maths, dates, sources.
<URL:http://www.merlyn.demon.co.uk/TP/BP/Delphi/jscr/&c, FAQ items, links.
Aug 26 '06 #5

P: n/a
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);

This message:
http://lists.evolt.org/archive/Week-...22/182514.html
suggests that Mozilla and IE behave differently on windows.
Phil.
Aug 26 '06 #6

P: n/a
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.
Aug 27 '06 #7

This discussion thread is closed

Replies have been disabled for this discussion.