problem display foreign characters in javascript country/region dropdown ...

M

Melissa

Initially the form is loaded using ASP and HTML and the ü codes
display the characters correctly. I have the values stored in a
javascript array so that I can more easily and dynamically change the
state/region dropdown when the country changes. However, when my
javascript code tries to add the new option and print out new region
the literal value such as "Würrtemberg" is displayed instead of
the special character.

Can this be done in javascript?
(I saw a previous old question about this, but no reply.)

Thanks.
Melissa Mussitsch
 
L

Leo Meyer

Initially the form is loaded using ASP and HTML and the ü codes
display the characters correctly. I have the values stored in a
javascript array so that I can more easily and dynamically change the
state/region dropdown when the country changes. However, when my
javascript code tries to add the new option and print out new region
the literal value such as "Würrtemberg" is displayed instead of
the special character.

Hi Melissa,

the HTML entities like ü are not properly evaluated by the JavaScript
interpreter when assigning them as values of option items. This may be
browser dependent.
You can try to encode the values as hex unicode codepoints. In the example,
this would be W\u00FCrrtemberg. (By the way, the correct spelling is
"Württemberg" with one r and two t. It's close to where I live ;-)
It should be fairly easy to parse the contents of your array, detect the
entities and replace them with the equivalent unicode hex code. Or simply
hard-code them, doesn't matter as long as they are not loaded from a DB.

Hope this helps,

Leo
 
M

Melissa

the HTML entities like ü are not properly evaluated by the JavaScript
interpreter when assigning them as values of option items. This may be
browser dependent.
You can try to encode the values as hex unicode codepoints. In the example,
this would be W\u00FCrrtemberg. (By the way, the correct spelling is
"Württemberg" with one r and two t. It's close to where I live ;-)
It should be fairly easy to parse the contents of your array, detect the
entities and replace them with the equivalent unicode hex code. Or simply
hard-code them, doesn't matter as long as they are not loaded from a DB.

Hope this helps,

Leo

Leo,

Thank you so much for your response. I think this gets me closer and
at least I know I'm not losing my mind!
However, do you know of a formula to convert the HTML entity to the
unicode hex code? I didn't find anything in my search and know in my
instance that the number of characters I need to convert is too many to
hard-code. I was testing with the JavaScript charCodeAt function but
don't think this is getting me what I need and I don't fully understand
unicode versus unicode hex versus ascii and then html entities, etc.
It gets very confusing to me.

Thanks.
Melissa
 
M

Melissa

Thank you so much for your response. I think this gets me closer and
at least I know I'm not losing my mind!
However, do you know of a formula to convert the HTML entity to the
unicode hex code? I didn't find anything in my search and know in my
instance that the number of characters I need to convert is too many to
hard-code. I was testing with the JavaScript charCodeAt function but
don't think this is getting me what I need and I don't fully understand
unicode versus unicode hex versus ascii and then html entities, etc.
It gets very confusing to me.

Thanks.
Melissa- Hide quoted text -- Show quoted text -


Leo - actually I think I've realized that I need to convert my html
entity decimal value to hex. parseInt is not working the way I want it
to at the moment, but I'll work through that. I think I'm just tired!

Thanks.
Melissa
 
L

Leo Meyer

Melissa said:
Thank you so much for your response. I think this gets me closer and
at least I know I'm not losing my mind!

Take it easy ;-)
Leo - actually I think I've realized that I need to convert my html
entity decimal value to hex. parseInt is not working the way I want it
to at the moment, but I'll work through that. I think I'm just tired!

It's fairly simple. You could define a function that takes a string.
Get the position of an HTML entity ("&#").
If found (position > -1), append characters up to this position to the
result variable.
Collect characters up to the next ";" or the end of the string.
Check that the characters are in fact a number.
Convert the number to hex, prefix with 0's if necessary. Append to the
output.
Remove chars from the input string up to the end of the entity.
Repeat until no more entities can be found.
Return the result string.

Such type of string parsing is fairly common practice. Unfortunately, it is
rarely taught. If you can't get it to work, post your code here, and we'll
se what we can do ;-)

Leo
 
B

Bart Van der Donck

Melissa said:
Initially the form is loaded using ASP and HTML and the ü codes
display the characters correctly. I have the values stored in a
javascript array so that I can more easily and dynamically change the
state/region dropdown when the country changes. However, when my
javascript code tries to add the new option and print out new region
the literal value such as "Würrtemberg" is displayed instead of
the special character.

alert ('Schöne Mädchen in Würtemburg!'
.replace(/(&)(#)(\d{1,})(;)/g,
function (tot,amp,cr,cp,sem) {
return String.fromCharCode(cp)
}
)
)
 

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

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top