469,293 Members | 1,335 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,293 developers. It's quick & easy.

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

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('qwerqwerq werqw'));
bar.appendChild(document.createTextNode('GGGG »asdfasdf« HHHH
U+00BB HHH' + CHEV_LEFT.firstChild.nodeValue + ' ' +
CHEV_RIGHT.firstChild.nodeValue));
}
::::: foo.js end :::::

Nov 9 '06 #1
3 7364
st**************@gmail.com wrote:
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.

--

Martin Honnen
http://JavaScript.FAQTs.com/
Nov 9 '06 #2
VK

st**************@gmail.com wrote:
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);

Nov 9 '06 #3
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

Nov 9 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Hardkmel | last post: by
2 posts views Thread by Goldfisch1980 | last post: by
10 posts views Thread by pekka niiranen | last post: by
reply views Thread by John Henry | last post: by
reply views Thread by Pnooka | last post: by
reply views Thread by oozyscab via AccessMonster.com | last post: by
reply views Thread by Thore Harald =?iso-8859-1?Q?H=F8ye?= | last post: by
1 post views Thread by Alexey Smirnov | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by Geralt96 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.