最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

character encoding - Icelandic language messed up in javascript alert box - Stack Overflow

programmeradmin0浏览0评论

i have an alert box which i want to show some icelandic text but its not showing it

<script>
function check() {
alert("Þú verður að vera skráð/ur inn til þess að senda skilaboð");
}
</script>

it is showing the alert box but the text is messed up :(

Þú verður að vera skráð/ur inn til þess að senda skilaboð

any help please :(

i have an alert box which i want to show some icelandic text but its not showing it

<script>
function check() {
alert("Þú verður að vera skráð/ur inn til þess að senda skilaboð");
}
</script>

it is showing the alert box but the text is messed up :(

Þú verður að vera skráð/ur inn til þess að senda skilaboð

any help please :(

Share Improve this question asked Jun 21, 2012 at 13:24 baig772baig772 3,49812 gold badges54 silver badges98 bronze badges 4
  • 1 Are you using UTF-8 encoding in your page and in your text? – Diodeus - James MacFarlane Commented Jun 21, 2012 at 13:27
  • What's your charset? Works fine here jsfiddle/j08691/49Dec – j08691 Commented Jun 21, 2012 at 13:27
  • <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> is how i have set my charset – baig772 Commented Jun 21, 2012 at 13:29
  • now alert is ok but my other icelandic text on my website is messed up. cannot risk of whole website for one alert :( – baig772 Commented Jun 21, 2012 at 13:36
Add a ment  | 

4 Answers 4

Reset to default 6

Today the web uses many international languages and has settled on using UTF-8 (a flavour of unicode) for character encoding. This is important.

You are using iso-8859-1, the MS Windows character set. If you have Word 2007 or 2010 you have the option of re-saving your text as UTF-8. If you've ever seen ????? or instead of text on someone's web site, it's due to the wrong encoding type.

<meta http-equiv="Content-type" content="text/html; charset=UTF-8"/>

Always use UTF-8 end-to end. Do not use 8859-1 or Windows 2151 encoding.

See:

The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

ISO-8859-1 vs UTF-8?

Character encodings and the beauty of UTF-8

Let's do it in html5 manner :)

<!doctype html>
<html>
   <head>
       <meta charset = "utf-8" />
   </head>

encoding hazards my best guesses: check if...

  1. the js-file is stored properly encoded (UTF8) on your server
  2. the server delivers the JS file with proper encoding header (HTTP/1.1 about encoding)
  3. ... @Diodeus is right

What you see displayed by the alert is UTF-8 encoded text misinterpreted as windows-1252 encoded. (Windows-1252 is a Microsoft extension to ISO-8859-1.)

If your pages are ISO-8859-1 encoded, as they apparently are, then this applies to the script element content too. There is something odd going if the code you posted does not work. Are you sure the element is really inside a normal page of yours where Icelandic characters work OK? You should not try this fix the situation with a shot in the dark like changing encodings without knowing what is going on.

I’m just making a guess: the alert() invocation is really in an external .js file, which is UTF-8 encoded but treated by browsers as windows-1252 encoded. Then there are two alternative fixes: 1) open that file in an editor and save it as windows-1252 or ISO-8859-1 encoded; or 2) modify server settings to declared UTF-8 for .js files or (less reliably) add charset=utf-8 attribute to the script element.

Alternatively, if the alert() invocation is really inside a script element in an HTML file, then perhaps this file is really UTF-8 encoded but you don’t observe other problems because the content of the file does not otherwise contain Icelandic characters. In this case, it is best to open the HTML file in your authoring program and change its encoding to windows-1252 or ISO-8859-1.

发布评论

评论列表(0)

  1. 暂无评论