473,398 Members | 2,088 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,398 software developers and data experts.

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

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
XP).

Result:

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 ==
'A\r\nB\r\nC'.

Suppose we have the following html code.
<textarea id="foo">A
B
C</textarea>
<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.
}

Thanks.

Jan 11 '07 #1
1 11969
In article <11**********************@i39g2000hsf.googlegroups .com>,
HopfZ <ho******@gmail.comwrites
>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
XP).
<snip>

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
--
John Harris
Jan 11 '07 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Christopher Finke | last post by:
I have a site wherein the clients can update the individual pages by editing the text of that page in a <textarea>. Then, when the page is displayed, all of the newlines (\n) are converted to HTML...
1
by: Porthos | last post by:
Hello all, Is there a facet pattern that will allow for the inclusion of a newline or carriage return to occur within a tag? From the W3C schema document and from previous posts I've read that ...
1
by: dbee | last post by:
Hi, So I'm having a problem with disappearing newlines. I import the newlines from a file into my shell script fine. But then I process the text and the url_encode comes out the other end with...
6
by: Phil Endecott | last post by:
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...
12
by: yoyo | last post by:
So I'm trying to make an application that currently works with MySql, Postgre, etc... work with DB2. THe problem I have, is, the varchar column only goes to 32k. CLOB goes bigger, but at a major...
2
by: Edward K. Ream | last post by:
Hello all, I recently ran across a situation in which sax.saxutils.quoteattr did not work as I expected. I am writing Leo outlines as opml files http://en.wikipedia.org/wiki/OPML which forces...
4
by: dk9 | last post by:
The Situation: I'm working in my browser at a secured site (https). At the same time I need my application to get and parse some urls from that same site. What I've tried: I've login with my...
14
by: Rob | last post by:
I am trying to perform client-side input validation for a textarea to determine that the number of characters doesn't exceed a certain length. Currently, I am just using str.length, but if the...
4
by: lihao0129 | last post by:
Hi, folks: I recently went through a strange problem with my Javascript code, say: I have a string variable which are from a 'textarea' element and I want to remove the trailing newlines inside...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.