javscript: specific chars decode problem

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Dmitry

    #1

    javscript: specific chars decode problem

    Code sample:

    <script>
    alert("&#176;")
    document.write( "&#176;")
    </script>

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

    unescape() does not work.

    Thank you, Dmitry


  • Stephen Chalmers

    #2
    Re: javscript: specific chars decode problem

    Dmitry <nospam@nospam. com> wrote in message
    news:cvqa16$16o o$1@news.peterl ink.ru...[color=blue]
    > Code sample:
    >
    > <script>
    > alert("&#176;")
    > document.write( "&#176;")
    > </script>
    >
    > Question: How can I force alert() to produce the same
    > result as document.write( ) ?
    >
    > unescape() does not work.
    >
    > Thank you, Dmitry
    >[/color]
    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.fromChar Code( parseInt( "&#176;".replac e( /\D/g, "" ) ) )

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

    function htmlToChar(char Code)
    {
    return String.fromChar Code( parseInt( charCode.replac e( /\D/g, "" ) ) );
    }

    alert( htmlToChar("&#1 76;") );

    ....unless someone knows another way that eludes me...
    --
    S.C.






    Comment

    • Dmitry

      #3
      Re: javscript: specific chars decode problem

      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


      Comment

      Working...