SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast angelinetky's Avatar
    Join Date
    Oct 2005
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    special character in javascript

    Hi all,

    Im doing an html page which calls the javascript function. In the function, i include the "text" field which inside the text includes the " ' " (apostrophe).
    my code is as following:

    javascript:link_onClick('<%=_MsgId%>', 'text','<%=Utils.formatTwoDecimal(Price())%>')

    Because of the " ' ", so the parsing of javascript encountered error.
    How to solve this problem?

    Thanks,

    Regards,
    Angie

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Escape the apostrophe by a backslash character: \'
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Enthusiast angelinetky's Avatar
    Join Date
    Oct 2005
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Can u give me an example?

    for example my text will be "testing's 123" and this text will be retrieved from database

    Thanks.

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    For a static value, you'd write "testing's 123" (using double quotes) or 'testing\'s 123'.

    For a value retrieved from the database (I'm assuming ASP/VBScript here), you could escape it like this:
    '<%=Replace(text, "'", "\'")%>'
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Enthusiast angelinetky's Avatar
    Join Date
    Oct 2005
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    '<%=Replace(text, "'", "\'")%>'

    It's not working, it assumes the ' is one part of the javascript.

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Are you using ASP/VBScript?

    What does the actual code look like? In you example, you use 'text' as a literal value.
    Birnam wood is come to Dunsinane

  7. #7
    SitePoint Enthusiast angelinetky's Avatar
    Join Date
    Oct 2005
    Posts
    42
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    im using java.

    text="testing's 123";

    <a href="javascript:link_onClick('<%MsgId%>', '<%=text%>','<%=Utils.formatTwoDecimal(Price)%>')">link</a>

  8. #8
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,158
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, then you 'll have to use Java's function for substituting "'" into "\'" instead. My Java is too rusty at the moment, but I'm sure you'll know how to do it.
    Birnam wood is come to Dunsinane


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
  •