javscript: specific chars decode problem

D

Dmitry

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
 
S

Stephen Chalmers

Dmitry said:
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
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...
 
D

Dmitry

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
 

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

Ask a Question

Similar Threads


Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top