SitePoint Sponsor

User Tag List

Results 1 to 4 of 4

Thread: Quoting problem

  1. #1
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Quoting problem with templating system

    Hi,
    I am working with a templating system, that is essentially a black box. I cannot configure it. I want to incorporate the expanded template codes into my JavaScript. For example the template code $123 might expand into "Hello World".

    <script>
    var field = '$123';
    document.write ('field');
    </script>

    This works fine but if the template code expands into "Hello Fred's World" then I get a syntax error as the apostrophe in Fred's is interpreted as the end of the string.
    I need this to work for both apostrophes and inverted commas. Does anyone have a solution? Is there a way to escape these characters before their assignment in JavaScript.
    Thanks for any help.
    Last edited by perlcgi; Dec 9, 2002 at 07:16.

  2. #2
    SitePoint Guru dale_burrell's Avatar
    Join Date
    Aug 2002
    Location
    Wellington, New Zealand
    Posts
    861
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I think its a backslash '\' before the char you want to escape.
    If you aren't living life on the edge
    - you're taking up too much space
    Creative Dreaming Ltd / Ask The Local / Amanzi Travel

  3. #3
    SitePoint Member
    Join Date
    Dec 2002
    Posts
    2
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Dale,
    You are right in that the slash will escape a quote character, however I will need to programatically insert the slash, which brings us back to the original problem, as the code to insert the slash will break on the first quote
    it encounters.

    <script>
    var field = '$123'; // field is now 'Hello Fred's World'
    field = field.replace(/"/g,\");
    document.write ('field');
    </script>

    This code will break because the variable field is set to
    'Hello Fred's World' an invalid string.

    Thanks for any assistance.

  4. #4
    SitePoint Member
    Join Date
    Dec 2002
    Location
    Clearwater, FL
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Quotes and escape characters

    It works w/ regular quotes, if it's possible to get it that way:

    var field = "Hello Fred's World";
    document.write (field);


    Alternatively, see the following site which has good examples of using an escape character for just such a case.
    (I am not affiliated with the site - it is simply one that I have found helpful in learning JS).

    http://www.htmlgoodies.com/beyond/escapecharacter.html

    I sympathize with your difficulty with quotes; here's what I recently had to do to get variables into a quoted parameter:

    @qry=' ... WHERE [Last Name]='''+@lname+''''+'and [First Name]='''+@fname+''''

    Bill


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
  •