Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > two types of newlines (\n and \r\n) and browser

Thread Tools

two types of newlines (\n and \r\n) and browser

Posts: n/a
Not a question.

I tested how two kinds of newlines (\n and \r\n) interact with three
browsers: Fx (Firefox 2), Op (Opera 9), Ie (IE 7) (all three on Windows


The string 'A\nB\r\nC' contains both forms of newlines.

var s1 = 'A\nB\r\nC';
document.getElementById('textarea_id').value = s1;
Above code will put that string to a textarea. In all three browsers,
the textarea will then look like:
|A |
|B |
|C |

Then let's do this:
var s2 = document.getElementById('textarea_id').value;
Now, in all three browsers, s2 is not the same as s1.
In Fx, we have s2 == 'A\nB\nC'. In Op and Ie, we have s2 ==

Suppose we have the following html code.
<textarea id="foo">A
<textarea id="bar"></textarea>

Open it in browser and type A, Enter, B, Enter and C in the textarea
with id "bar" and click a button which does:
s3 = document.getElementById('foo').value;
s4 = document.getElementById('bar').value;

Now, in Fx, s2 == s3 == s4 == 'A\nB\nC'. In Op and Ie, s2 == s3 == s4
== 'A\r\nB\r\nC'.

In most cases, it's not needed to care about two kinds of newlines. But
in some cases, it is.

Suppose we want to take text from a textarea and count the number of
letters in each line and show the numbers.
For example, If I fill the textarea as below:
|My name is |
|Hiro Nakamura. |
|I have a message.|
, the result should be:
|10 |
|14 |
|17 |

The corresponding javascript should be:

function lines2Numbers(s){
var li = s.split('\n');
for(var i=0;i<li.length;i++){ li[i] = li[i].length };
return li.join('\n');

button1.onclick = function(){
var i = document.getElementById('input');
var o = document.getElementById('output');
// o.value = lines2Numbers(i.value); No.
o.value = lines2Numbers(i.value.replace(/\r\n/g,'\n')); // Yes.


Reply With Quote
John G Harris
Posts: n/a
In article <(E-Mail Removed) .com>,
HopfZ <(E-Mail Removed)> writes
>Not a question.
>I tested how two kinds of newlines (\n and \r\n) interact with three
>browsers: Fx (Firefox 2), Op (Opera 9), Ie (IE 7) (all three on Windows


HTML allows three kinds of line terminators : CR, LF, and CRLF. In
javascript these are \r, \n, and \r\n.

I'm not surprised that text held in a DOM object uses whatever line
terminator is convenient for the browser.

John Harris
Reply With Quote

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Argument type of function and safe types and types, arguments canhandle safely ittium C++ 4 12-09-2011 11:05 AM
strip newlines and blanks Python 5 05-02-2006 11:54 AM
jython/wlst: How to avoid newlines in output from "cd" command (and others) Python 0 12-23-2005 07:25 PM
Validating Newlines and Carriage Returns via Schema Porthos XML 1 07-26-2005 11:13 PM
[NEWBIE] Multiline match on two files ignoring newlines, tabs & blank chars Ga Perl Misc 1 12-15-2003 11:31 AM