SitePoint Sponsor

User Tag List

Results 1 to 6 of 6

Hybrid View

  1. #1
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Special Charators

    Hi Folks,

    Is there a way to use them in strings?

    Can I search for them and replace them with an ascii value or silimar somehow?

    Thanks in advace

  2. #2
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What special characters? If you want to include a character that you cannot input directly from your keyboard, or cannot be represented using the character encoding you use, you can use escape notation.

    Octal escape notation:
    Code:
    var copyright = "\251 2010";
    Unicode escape notation:
    Code:
    var copyright = "\u00a9 2010";
    Literal notation:
    Code:
    var copyright = "© 2010";
    For characters that are special to JavaScript, escape them with a preceding backslash:
    Code:
    var s1 = "My \"new\" car";
    var s2 = 'Jane\'s car';
    Birnam wood is come to Dunsinane

  3. #3
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply.

    Its from a user input field so I'm not sure where the special chars will be and what they are.

    I query the database and create my html, could I use a regular expression or similar when doing this to convert all the special chars to javascript friendly text?

    For example this code causes an error:

    homepage[0] += "example - example"

    Many thanks

  4. #4
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by mattastic View Post
    Its from a user input field so I'm not sure where the special chars will be and what they are.
    I don't understand.
    If it's from an input field it's already a string!

    Quote Originally Posted by mattastic View Post
    I query the database and create my html, could I use a regular expression or similar when doing this to convert all the special chars to javascript friendly text?
    Do you mean the text comes from a database and you're creating JavaScript containing that text using server-side programming? In that case you need to escape special characters in the server-side script that generates the code. It's not a JavaScript problem.

    Quote Originally Posted by mattastic View Post
    For example this code causes an error:

    homepage[0] += "example - example"
    Why should that cause an error (unless homepage[0] is undefined)?

    When asking for help online it's always a good idea to be as explicit as possible about what you're trying to do, and why. Providing code examples of existing code also helps. I don't quite understand what you are trying to do, or how.
    Birnam wood is come to Dunsinane

  5. #5
    SitePoint Addict
    Join Date
    Jun 2006
    Posts
    250
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for your reply.

    The javascript thats causing an error is below.

    Can you help?

    Thankyou

    Code:
    homepage[0] = "<h2 class='draggable'>Bring n buy</h2><div class='container'>"
    homepage[0] += '<ul>'
    homepage[0] += "<li><a href='index.cfm?area=marketplace/index&page=view-forum&forumid=267'>Kingsize bed frame</a></li>"
    
    Error: unterminated string literal
    Source File: http://intranet/portal.cfm
    Line: 24, Column: 16
    Source Code:
     homepage[0] += "<li><a href='index.cfm?area=marketplace/index&page=view-forum&forumid=267'>

  6. #6
    SitePoint Author silver trophybronze trophy

    Join Date
    Nov 2004
    Location
    Ankh-Morpork
    Posts
    12,159
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Do you have this JavaScript code between <script> and </script> rather than in an external script file (<script src="..."></script>?

    If so, the problem is most likely the closing tags you have in your strings.

    In HTML (which includes pretend-XHTML) the script element type has a content model of CDATA, which behaves a bit oddly. One oddity is that the first occurrance of the character sequence '</' followed by a name-start character will be interpreted as </script> and thereby close the script tag.

    To prevent this you need to escape that character sequence, which you can do by inserting a backslash character between the '<' and the '/'. That 'escapes' the '/' character.

    Code JavaScript:
    homepage[0] += "<li><a href='index.cfm?area=marketplace/index&page=view-forum&forumid=267'>Kingsize bed frame<\/a><\/li>"

    Without the backslash characters in the end tags, the script tag is closed after 'bed frame' which means the string constant isn't properly terminated.
    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
  •