Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > With UTF-8 charset, how do I do .appendChild( doc.createTextNode('«') );

Reply
Thread Tools

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

 
 
stephen.cunliffe@gmail.com
Guest
Posts: n/a
 
      11-09-2006
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 :::::

 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      11-09-2006
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
 
VK
Guest
Posts: n/a
 
      11-09-2006

(E-Mail Removed) 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);

 
Reply With Quote
 
stephen.cunliffe@gmail.com
Guest
Posts: n/a
 
      11-09-2006
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM



Advertisments