473,320 Members | 2,094 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Character codes for newlines in textareas

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
6 2664
*** 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
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
*** 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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Dominik Jain | last post by:
Hi! We hope somebody can help me with the following: We have a form through which unicode data might be submitted. We need to be able to detect when this happens and, most importantly, we need...
21
by: aegis | last post by:
7.4#1 states The header <ctype.h> declares several functions useful for classifying and mapping characters.166) In all cases the argument is an int, the value of which shall be representable as an...
15
by: Craig Wagner | last post by:
I have a situation where I have a series of character codes stored in the database. In some cases they are the ASCII value of the character, in other cases they are the > 127 character code value...
0
by: kurotsuke | last post by:
I need to convert a sequence of keys presses on the keyboard into the corresponding character code (UNICODE). I'm intercepting the KeyUp event (using an external hooking library) and need to get...
18
by: Marcel Saucier | last post by:
Hello, I want to use the above characters codes chart but I dont know how to set the typeface (documentation: The characters that appear in Windows above 127 depend on the selected typeface). ...
9
by: simchajoy2000 | last post by:
Hi, I know what the ASCII Character Codes are for the 2nd and 3rd powers in VB.NET but I can't find the 6th power anywhere - does anyone know what it might be or if it even exists? Joy
1
by: Dwight | last post by:
Hi all, How do you enter the character code for the symbol "2/3". I can enter the codes for "1/2", "3/4", but not for any other fraction. tia Dwight
3
by: jake | last post by:
I am new to xml. I have a routine that parses xml files using a regular XmlReader class. Unfortunately, the XmlReader chokes (throws an exception) on character codes such as "&Eacute;". I...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.