Split function in Firefox
I've been using the split function to separate the lines in an textarea field (denoted by a CRLF) into an array of strings, using the following code:
This works fine in Internet Explorer, but I've just noticed that it doesn't work in Firefox.
It always return an array with one item, the first line.
Can anyone help me out here?
Do a replace before the split:
The line-break/carriage-return is processed differently between browsers
Thanks Craig, that now works in Firefox, but it's now working differently in IE. It removes empty lines.
The following did return 7 lines, but now returns 4!
No worries, fixed it by doing the following:
if (address.value.search('\r\n') >= 0)
var lines = address.value.split('\r\n');
var lines = address.value.split('\n');
Thanks for the above when I was trying to explain weird String.split() behaviour.
This may be a more general solution, though I have only tested it with newlines in the form \r\n on Firefox and IE.
This just returns the first line (whether blank or not) from the text containing the newlines. You would need to set up a pointer to step through for the other lines. But without the == 0 test I think the method above will lose the first line if it happens to be blank.
v = text_containing_newlines;
v = v.replace(/\r\n/, '\n');
if (v.search(/\n/) == 0) v = ''; // Else IE will ignore the blank line and fetch the next one
else if (v.search(/\n/) > 0) v = v.split(/\n/);
Users Browsing this Thread
There are currently 1 users browsing this thread. (0 members and 1 guests)