tags:

views:

12905

answers:

6

Is \n the universal newline character sequence in Javascript for all platforms? If not, how do I determine the character for the current environment?

I'm not asking about the HTML newline element (<BR/>). I'm asking about the newline character sequence used within JavaScript strings.

+8  A: 

Yes, it is universal.

Although '\n' is the universal newline characters, you have to keep in mind that, depending on your input, new line characters might be preceded by carriage return characters ('\r').

Sinan Taifour
easy layup on this one
Saul Dolgin
\n is the universal *line feed* character (LF). The exact newline byte sequence depends on the platform (\r\n, \n, or \r: http://en.wikipedia.org/wiki/Newline). And *that's* the question landon is asking. You're contradicting yourself when you first say it's the universal newline character, and then say it may be preceded by a CR. Which one is it?
mercator
+1  A: 

yes use \n, unless you are generating html code, in which you want to use <br>

Finer Recliner
A: 

I believe it is -- when you are working with JS strings.

If you are generating HTML, though, you will have to use <br /> tags (not \n, as you're not dealing with JS anymore)

Pascal MARTIN
+12  A: 

I've just tested a few browsers using this silly bit of JavaScript:

<!doctype html>
<script>
function bar(){
    baz = document.getElementById('foo').value;
    alert((baz.match(/\r/) && 'CR') + ' ' + (baz.match(/\n/) && 'LF'));
    document.getElementById('foo').value = "foo\nbar";
}
</script>
<body onload="bar()">
<form><textarea id="foo" name="foo">

</textarea>
<input type="submit">
</form>
</body>

IE8 and Opera 9 on Windows use \r\n. All the other browsers I tested (Safari 4 and Firefox 3.5 on Windows, and Firefox 3.0 on Linux) use \n. They can all handle \n just fine when setting the value, though IE and Opera will convert that back to \r\n again internally. There's a SitePoint article with some more details called Line endings in Javascript.

Note also that this is independent of the actual line endings in the HTML file itself (both \n and \r\n give the same results).

When submitting the form, all browsers canonicalize newlines to \r\n (%0D%0A in URL encoding).

I don't think you really need to do much of any determining, though. If you just want to split the text on newlines, you could do something like this:

lines = foo.value.split(/\r\n|\r|\n/);
mercator
A: 

The \n is just fine for all cases I've encountered. I you are working with web, use \n and don't worry about it (unless you have had any newline-related issues).

zzandy
A: 

There is no universal newline character as you've discovered. Because of that the internet is full of apps and operating systems that speak and read both types of newline. For example MIME (email) specifies you should use the \r\n newline sequence, but mostly works just fine with \n.

In many ways this is a good thing, but yes it causes inconsistencies in things like form inputs... it's easy to handle though (with javascript, assuming form is called inputform and textarea is called multilineinput):

var lines=document.forms['inputform'].multilineinput.value.replace(/\r\n/g,"\n").split("\n");

The algorithm is pretty simple - replace any \r\n with just \n, and then split by that character.

Rudu