SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    replace issue with IE only

    Im doing some AJAX stuff where Im sending the contents of a textarea to a server side app, formatting it and then returning it and displaying it. I have it working perfectly in every browser EXCEPT freaking IE (testing with 6.0). Heres some code and what's happening:

    var articlebody = document.getElementById("article").value;

    articlebody = articlebody.replace( new RegExp( "\\n", "g"), "|" );
    I have to remove the new line characters from the text AND I need a flag on my server side app to tell me where the lines end so I am doing a replace of the new line chars with pipes "|"

    Now this works flawlessly in FF but in IE it is adding the pipes along side the new line chars . in other words when I do an alert on the outbound data in FF I get this:

    some text from the textbox|another line from the text box|etc etc etc|

    but in IE the same alert gives me this:

    some text from the textbox
    |
    another line from the text box
    |
    etc etc etc
    |

    Needless to say it's screwing up the logic i have running on the server side app..

    Any ideas are truely appreciated!

  2. #2
    He's No Good To Me Dead silver trophybronze trophy stymiee's Avatar
    Join Date
    Feb 2003
    Location
    Slave I
    Posts
    23,423
    Mentioned
    2 Post(s)
    Tagged
    1 Thread(s)
    try replacing \\r\\n

  3. #3
    SitePoint Wizard gold trophysilver trophybronze trophy dc dalton's Avatar
    Join Date
    Nov 2004
    Location
    Right behind you, watching, always watching.
    Posts
    5,431
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yuppers, that does do it but it foul up the text for FF so I had to do a browser sniff. Anything that wasnt IE got my replace, any IE browser I used yours ...... works great.

  4. #4
    SitePoint Wizard
    Join Date
    Mar 2001
    Posts
    3,537
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Carriage returns, line feeds, textarea, Firefox

    1) You don't need to use the RegExp constructor. You only need to use the RegExp constructor when you want to create a regexp from a string type, e.g. you have a variable that is a string type and you want to create a regex from it. Otherwise, in js you can just use a regex literal:

    articlebody = articlebody.replace( /\r\n/g), "|" );

    That way you don't have to worry about the double backslashes. In js, the opening '/' and closing '/' are similar to quotes around a string, and they specify a regex literal.

    2) Browser sniffing is not foolproof because browser vendors who design their browsers with javascript support they think is equivalent to IE or FF will identify themselves as those browsers--in order to be included by scripts like yours that sniff out browsers. When the features in IE's browser caught up to Netscape's, IE used that trick to be included in scripts that allowed Netscape, and as a result IE still identifies itself as Mozilla today.

    In any case, a regular expression can be made to look for alternate things, like an IE carriage return(\r\n) or a Firefox carriage return(\n):

    articlebody = articlebody.replace( /\r\n|\r|\n/g, "|" );

    (I threw a mac os carriage return(\r) in there, too.)

    In a textarea on winIE6 a carriage return is \r\n(as expected), but in a textarea on winFF1.0.7 a carriage return is a \n. I thought carriage returns were os specific, as below:
    windows: \r\n
    mac: \r
    unix: \n
    but apparently FF converts the \r\n to \n (on windows).
    Last edited by 7stud; Jan 13, 2007 at 04:13.


Bookmarks

Posting Permissions

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