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

Discussion in 'Javascript' started by stephen.cunliffe@gmail.com, Nov 9, 2006.

  1. Guest

    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 :::::
     
    , Nov 9, 2006
    #1
    1. Advertising

  2. 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/
     
    Martin Honnen, Nov 9, 2006
    #2
    1. Advertising

  3. VK Guest

    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);
     
    VK, Nov 9, 2006
    #3
  4. Guest

    > 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, 2006
    #4
    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. Matt
    Replies:
    3
    Views:
    531
    Tor Iver Wilhelmsen
    Sep 17, 2004
  2. Avi U
    Replies:
    2
    Views:
    589
    Martin Honnen
    Jan 13, 2004
  3. Danny
    Replies:
    11
    Views:
    851
  4. Martin Honnen

    Re: DOM CreateTextNode with Unicode

    Martin Honnen, Feb 29, 2008, in forum: XML
    Replies:
    0
    Views:
    390
    Martin Honnen
    Feb 29, 2008
  5. optimistx

    javascript charset <> page charset

    optimistx, Aug 14, 2008, in forum: Javascript
    Replies:
    2
    Views:
    286
    optimistx
    Aug 15, 2008
Loading...

Share This Page