www.webdeveloper.com
Results 1 to 5 of 5

Thread: Split function in Firefox

  1. #1
    Join Date
    Jul 2005
    Posts
    3

    Question 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:

    Code:
    address.value.split('\r\n')
    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?

    TIA

  2. #2
    Join Date
    Jan 2004
    Location
    Washington DC
    Posts
    1,107
    Do a replace before the split:

    var str=address.value.replace(/\r/g,"");
    str.split(/\n/);

    The line-break/carriage-return is processed differently between browsers
    Bittersweet web development.

  3. #3
    Join Date
    Jul 2005
    Posts
    3
    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!

    Edwin Road



    MANCHESTER
    Lancashire
    M11 3ER

  4. #4
    Join Date
    Jul 2005
    Posts
    3
    No worries, fixed it by doing the following:

    Code:
    if (address.value.search('\r\n') >= 0)
    {
      var lines = address.value.split('\r\n');
    }
    else
    {
      var lines = address.value.split('\n');
    }
    Thanks again

  5. #5
    Join Date
    Oct 2005
    Posts
    1

    Further thoughts

    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/)[0];

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles