With UTF-8 charset, how do I do .appendChild( doc.createTextNode('«') );

  • Thread starter stephen.cunliffe
  • Start date
S

stephen.cunliffe

I hope the subject doesn't get escaped, but I'll try to clarify here...

I have:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

in my head (and yes, I need to keep it UTF-8...)

But I have JavaScript (external file), that creates a node, and inserts
text in it...

foo.appendChild(document.createTextNode('Hello World'));

This works fine, as long as the characters are "normal"... if I try and
add a chevron («)for example, it won't work, no matter how I escape
it.

I can however, copy another node's value (rendered elsewhere on the
page), and insert it, and it works fine.

Long story short, other than my "hacky" solution above, how do I do
this?

I've attached some sample files below (again, I hope they don't get
escaped)...

Cheers,
Steve

::::: test.html start :::::
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>
</title>
<script src="foo.js"></script>
</head>
<body onload="doit();">
World
<br/>
Chevron Left <span id="CHEVRON_LEFT">«</span>
<br/>
Chevron Right <span id="CHEVRON_RIGHT">»</span>
</body>
</html>
::::: test.html end :::::

::::: foo.js start :::::
function doit(){
var bar = document.getElementsByTagName('body')[0];
var CHEV_LEFT = document.getElementById('CHEVRON_LEFT');
var CHEV_RIGHT = document.getElementById('CHEVRON_RIGHT');
bar.appendChild(document.createTextNode('qwerqwerqwerqw'));
bar.appendChild(document.createTextNode('GGGG »asdfasdf« HHHH
U+00BB HHH' + CHEV_LEFT.firstChild.nodeValue + ' ' +
CHEV_RIGHT.firstChild.nodeValue));
}
::::: foo.js end :::::
 
M

Martin Honnen

I have:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">

in my head (and yes, I need to keep it UTF-8...)

But I have JavaScript (external file), that creates a node, and inserts
text in it...

This works fine, as long as the characters are "normal"... if I try and
add a chevron («)for example, it won't work, no matter how I escape
it.
<script src="foo.js"></script>

How is the script file foo.js encoded? Does the server send a charset
parameter for the Content-Type header of your .js files? Otherwise use e.g.
<script type="text/javascript" src="foo.js" charset="UTF-8"></script>
to tell the user agent the encoding. charset="UTF-8" is just an example,
put the encoding there that you use for your files or at least for that
particular file.
 
V

VK

I have:
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
in my head (and yes, I need to keep it UTF-8...)

Only if it matches the actual page charset (Unicode), otherwise it is a
call for troubles.
This works fine, as long as the characters are "normal"... if I try and
add a chevron (?)for example, it won't work, no matter how I escape
it.

That's because - as hundreds prior you - you tried all imaginable
escape methods except the right one :-( :)

JavaScript operates with Unicode and Unicode only; respectively it
doesn't care of HTML entities or ASCII codes. Use Unicode escape
sequences and be happy ever after. Here is a small table of characters
coders around the world are starving for most oftenly (feel like start
charging for it :)

00A0 no-break space

00A4 currency sign
20AC Euro
00A3 British pound
00A5 Japanese yen

00AB left guillomet
00BB right guillomet

201C left double quote
201D right double quote

2018 left single quote
2019 right single quote

Unicode escape sequence has form \uXXXX where XXXX is hex value of
Unicode character.

This way to say make "word" inside guillomets:
var quote = "\u00ABword\u00BB";

And to make a string with non-breaking space:
var nobr = "Word\u00A0Word";

You also can use String.fromCharCode method for numeric values:
var EuroSign = String.fromCharCode(0x20AC);
 
S

stephen.cunliffe

JavaScript operates with Unicode and Unicode only; respectively it
doesn't care of HTML entities or ASCII codes. Use Unicode escape
sequences and be happy ever after.

Awesome, problem solved!

Cheers,
Steve
 

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

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top