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/);