javscript: specific chars decode problem

Discussion in 'Javascript' started by Dmitry, Feb 26, 2005.

  1. Dmitry

    Dmitry Guest

    Code sample:

    <script>
    alert("°")
    document.write("°")
    </script>

    Question: How can I force alert() to produce the same
    result as document.write() ?

    unescape() does not work.

    Thank you, Dmitry
     
    Dmitry, Feb 26, 2005
    #1
    1. Advertisements

  2. You need to extract the numeric value from the html code, then convert it to
    an integer whose value you convert to a single-character string.

    alert( String.fromCharCode( parseInt( "°".replace( /\D/g, "" ) ) )

    If you intend do this repeatedly, it's best to write a function:

    function htmlToChar(charCode)
    {
    return String.fromCharCode( parseInt( charCode.replace( /\D/g, "" ) ) );
    }

    alert( htmlToChar("°") );

    ....unless someone knows another way that eludes me...
     
    Stephen Chalmers, Feb 26, 2005
    #2
    1. Advertisements

  3. Dmitry

    Dmitry Guest

    Thank you, I've thought about something simular,
    but the best thing is to have a fn for all cases,
    ie the fn, which accept a string, possibly containing
    html char codes together with plain chars and returns
    decoded string.

    I'am not so good in regular expressions programming,
    so any help in writing this fn will be greately
    appreciated.

    Thanks, Dmitry
     
    Dmitry, Feb 27, 2005
    #3
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.