basic Q on enclosing curly quotation/quot characters into strings

Discussion in 'Javascript' started by Tim, Nov 4, 2011.

  1. Tim

    Tim Guest

    I know some programming though not much JavaScript and I have to
    modify an existing JavaScript program that has an array assignment
    statement like

    labels[0] = "This does work for the typical case.";

    I need to change the "does" to "doesn't" where the single quote needs
    to be the proper curly single quote, because the font used to display
    this is a nice serif font. But the following change, using the
    "decimal numeric character reference" for right single quote
    ’ does not work:

    labels[0] = "This doesn’t work for the typical case.";

    I've looked online for 30 minutes and have tried concatenation and
    eval, but that does not work either.

    I just need to make this one change and get back to some other work.
    Anyone have the answer?
     
    Tim, Nov 4, 2011
    #1
    1. Advertising

  2. 11/4/2011 9:41 PM, Tim wrote:

    > I need to change the "does" to "doesn't" where the single quote needs
    > to be the proper curly single quote, because the font used to display
    > this is a nice serif font.


    A better reason is that the curly quotes are proper punctuation. But the
    use of punctuation should be consistent; use the same punctuation style
    in JavaScript-generated texts as in static content.

    > But the following change, using the
    > "decimal numeric character reference" for right single quote
    > ’ does not work:
    >
    > labels[0] = "This doesn’t work for the typical case.";


    It works in XHTML, but in classic HTML, no character references are
    recognized in <script> element content. And of course it does not work
    in an external JavaScript file.

    You can use the JavaScript construct for including a character in a
    string literal: \u followed by exactly four hexadecimal digits. In this
    example,

    "This doesn\u2019t work for the typical case."

    Alternatively, you can enter the curly single quote as such, as a
    character, provided that the document's character encoding makes that
    possible (e.g., windows-1252 or utf-8).

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Nov 4, 2011
    #2
    1. Advertising

  3. Tim

    Timothy Gill Guest

    On Nov 4, 4:05 pm, "Jukka K. Korpela" <> wrote:

    > You can use the JavaScript construct for including a character in a
    > string literal: \u followed by exactly four hexadecimal digits. In this
    > example,
    >
    > "This doesn\u2019t work for the typical case."


    Jukka, thank you. This works fine for me. (And I agree with you,
    it's really about punctuation.)

    In the mean time, I found I have several other places where I have to
    make a word italic inside a string assigned to an array variable. Is
    there a similar easy way to do that? I found mention of some built-in
    methods like italic() and bold() that I assume I can use in an
    assignment statement, but have not tried this yet:

    word="this";
    labels[0] = "The 5th word in " + word.italics() + " sentence should
    appear in italics.";

    Anyway, thanks again.
     
    Timothy Gill, Nov 4, 2011
    #3
  4. Re: basic Q on enclosing curly quotation/quot characters intostrings

    On Fri, 04 Nov 2011 13:42:04 -0700, Timothy Gill wrote:

    > word="this";
    > labels[0] = "The 5th word in " + word.italics() + " sentence should
    > appear in italics.";


    > Anyway, thanks again.


    What you do is create embedded html markup in the string, eg labels[0] is
    stored as "The 5th word in <i>this</i> sentence should appear in italics."

    Whether it displays the stored text or the html interpretation of it
    depends on the context in which the string is being used.

    If you use labels[0] in an alert box, or assign it to the value of a
    button, for example, you will see the markup.

    However, if you assign it as the innerHTML of a <td>, <li>, <hN>, <p> etc
    element, it will display the word "this" italicised.

    eg see http://www.sined.co.uk/tmp/italics.htm and click on "a button"

    Rgds

    Denis McMahon
     
    Denis McMahon, Nov 4, 2011
    #4
  5. 11/4/2011 10:42 PM, Timothy Gill wrote:

    > In the mean time, I found I have several other places where I have to
    > make a word italic inside a string assigned to an array variable. Is
    > there a similar easy way to do that?


    No, it's quite different from using the \u notation to enter characters.
    Italics and bolding are stylistic presentation features, so they don't
    make a character difference. They do not belong to plain text, and a
    JavaScript string as such is plain text.

    A plain text string may get interpreted as non-plain text when processed
    by suitable software. So if you use, say

    var foo = "Hello <i>world</i>"

    in JavaScript, then use it to put content into an HTML element, e.g.

    document.getElementById('bar').innerHTML = foo

    (assuming markup like <div id=bar></div> for example), then web browsers
    will treat the string as marked-up text and render <i>world</i> by
    displaying the word world in italics.

    (Beware that tags like </i> cause problems if your JavaScript code
    appears inside an HTML document. Better put it in an external JavaScript
    file.)

    But on the other hand, if you do alert(foo), then the string is
    displayed literally.

    To be very exact, and very theoretical, I need to add that there _are_
    Unicode characters that denote italic letters, such as U+1D44E
    (mathematical italic small a). You don't want to use them, for a
    multitude of reasons:
    * They are meant for use in math texts, not general usage.
    * They have very limited support in fonts (you need something like the
    Code2001 font to get them rendered).
    * They are non-BMP characters (outside the "16-bit subspace" of
    Unicode), which means that JavaScript implementations need not support
    them (though modern implementations generally do).
    * Being non-BMP, they cannot be written directly using the \u notation,
    which takes just 4 hex digits. Instead, it needs to be written using a
    surrogate pair, e.g. as
    '\uD835\uDC4E'
    and this in turn means that such a character counts as two e.g. when
    computing the length of a string.

    --
    Yucca, http://www.cs.tut.fi/~jkorpela/
     
    Jukka K. Korpela, Nov 4, 2011
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Winstanley [Microsoft MVP ASP.NET]

    Re: validateRequest=&quot;false&quot; not working in web.config or page directive

    Phil Winstanley [Microsoft MVP ASP.NET], May 16, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    653
    Phil Winstanley [Microsoft MVP ASP.NET]
    May 16, 2004
  2. usr2003
    Replies:
    4
    Views:
    608
    usr2003
    Sep 19, 2003
  3. Frank Schmitt
    Replies:
    0
    Views:
    498
    Frank Schmitt
    Oct 14, 2003
  4. Ahti Legonkov
    Replies:
    0
    Views:
    518
    Ahti Legonkov
    Dec 12, 2003
  5. Ken Fine
    Replies:
    2
    Views:
    183
    Ken Fine
    Feb 24, 2004
Loading...

Share This Page