SitePoint Sponsor

User Tag List

Results 1 to 2 of 2
  1. #1
    SitePoint Enthusiast
    Join Date
    Nov 2005
    0 Post(s)
    0 Thread(s)

    Newlines in function arguments


    I'm having trouble making a javascript function call from HTML when newline characters are in the string. The actual HTML is written into the document dynamically from the Javascript with no apparent problem. The function to which the string is passed is intended to write the string into an HTML textarea so I need to preserve newline characters in a way the textarea can use.

    First of all, I tried this:
    commentsR = comments.replace(new RegExp( "\\n", "g" ), "\\r");

    Which does seem to place \r where expected, but the string call still fails with "unterminated string constant" and using the MS debugger and copying and pasting the string into a textpad reveals there are still line break characters in the string - I have no idea what these are or how to find and replace them.

    I tried using encodeURI which seemed to do the trick but when the string was passed back with decodeURI I found that as well as preserving the newlines, it had added a normal space character after each newline.

    Anyone help me out with what to do here?


  2. #2
    Unobtrusively zen silver trophybronze trophy
    paul_wilkins's Avatar
    Join Date
    Jan 2007
    Christchurch, New Zealand
    104 Post(s)
    4 Thread(s)
    Can you please post the function call that's giving the "unterminated string constant" and also a sample of the troublesome string, perhaps after you've run the escape() function over that string?

    It doesn't appear to be a newline problem, but more of an issue with opening and closing quotes.
    Programming Group Advisor
    Reference: JavaScript, Quirksmode Validate: HTML Validation, JSLint
    Car is to Carpet as Java is to JavaScript


Posting Permissions

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