basic Q on enclosing curly quotation/quot characters into strings

T

Tim

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?
 
J

Jukka K. Korpela

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).
 
T

Timothy Gill

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.
 
D

Denis McMahon

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
 
J

Jukka K. Korpela

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Staff online

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,052
Latest member
LucyCarper

Latest Threads

Top