/    Sign up×
Community /Pin to ProfileBookmark

Displaying special characters

There’s a common problem in Javascript when displaying non-English characters like tildes. Even though the charset of the document in which the javascript is located is set as UTF-8, tildes in javascript alerts will be displayed with weird characters instead.

I cannot use the intuitive HTML entities to fix this. For instance: á for á. The most spread solution I’ve found on the Net is to use their corresponding Unicode characters, for instance u00e1 for á. But this is not intuitive at all. Each special character has an apparently random hexadecimal character assigned and it’s harder to have in mind than the HTML entities. Moreover I don’t think this is a fine solution.

So, is there a better way to work around this problem?

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@rnd_meDec 15.2010 — use html entitles to code, and convert them to "hard-coded" unicode characters just prior to calling alert. you can even make an alertUni() function to do this intrinsically.
Copy linkTweet thisAlerts:
@JazztronikDec 16.2010 — You mean something like this, don't you?:

[code=php]function alertUni(text) {
var conversions = {atilde: ['á', 'u00e1'],
etilde: ['é', 'u00e9'],
...
}
for (char in conversions) {
text = text.replace(char[0], char[1]);
}
alert(text);
}

alertUni(text);[/code]


I haven't tried it, so I'm not sure if it's error-free.


Anyway, isn't there any built-in solution for this in Javascript?
Copy linkTweet thisAlerts:
@rnd_meDec 16.2010 — 
Anyway, isn't there any built-in solution for this in Javascript?[/QUOTE]


no. firstly, special chars alert fine for me in firefox, not sure about other browsers...

secondly, alert is a browser method, not a javascript method: it's up to each browser implementation to decide how to display text within the alert() mechanism.
Copy linkTweet thisAlerts:
@lmartinLMVauthorDec 16.2010 — It was happening to me in Firefox too.

Edit: I'm Jazztronik (my username at home). I always forget to switch from one to another.:o
×

Success!

Help @lmartinLMV spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...