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

utf 8 - How to encode UTF-8 character in javascript? - Stack Overflow

programmeradmin1浏览0评论

I have a problem with showing a value from mysql database. So value is saved as UTF-8 in the mysql database ( correctly ) , I am retrieving a JSON formated data to javascript (correctly) and then when I print the result in the javascript I don't see right signs as I am using Croatian alphabet.

I have put this in the head section:

<meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/> and in the script section:

<script type="text/javascript" charset="utf-8">

What can I do next?

I have a problem with showing a value from mysql database. So value is saved as UTF-8 in the mysql database ( correctly ) , I am retrieving a JSON formated data to javascript (correctly) and then when I print the result in the javascript I don't see right signs as I am using Croatian alphabet.

I have put this in the head section:

<meta name="http-equiv" content="Content-type: text/html; charset=UTF-8"/> and in the script section:

<script type="text/javascript" charset="utf-8">

What can I do next?

Share Improve this question asked Jun 27, 2012 at 14:42 user123_456user123_456 5,82526 gold badges87 silver badges142 bronze badges 5
  • 1 What server side view technology are you using? It needs to be set as charset attribute of the HTTP response Content-Type header over there. The meta tag is ignored on HTTP responses and is for example only used when you let your webbrowser save the HTML output to disk and then open it from disk by a file:// URI instead of a http:// URI. – BalusC Commented Jun 27, 2012 at 14:51
  • How do you mean what server side view technology am I using? I am getting data in JSON format and it has utf-8 sign visible – user123_456 Commented Jun 27, 2012 at 15:03
  • Uh, simply put, what are you using to produce HTML? PHP, ASP, JSP, JSF, Python, RoR, etc..? Or is it a static HTML file and are you using a HTTP server without any default builtin support for a view technology like Apache HTTPD? – BalusC Commented Jun 27, 2012 at 15:04
  • now you got me a bit confused. Page is written in static html, but I am using javascript as I have a google maps on it. So I am pulling data from the server in order to fill the data in google maps. I have just tried to write something into console.log() from javascript in croatian alphabet and it is working correctly so problem might be in when retreiving data from server. Now I see that JSON is not giving me the right value. So it is php problem – user123_456 Commented Jun 27, 2012 at 15:11
  • So, you're using PHP as view technology? Mention (and tag) that as such then. I've posted an answer. – BalusC Commented Jun 27, 2012 at 15:15
Add a ment  | 

1 Answer 1

Reset to default 1

The character encoding has to be set on the real HTTP response Content-Type header, not alone on the meta tag. The meta tag is ignored when the HTML output is retrieved by a HTTP request. In webbrowser's developer toolset as you can get by pressing F12 in Chrome/IE9/Firebug, you must be able to explore the HTTP response headers like below:

Based on the ments you're apparently using PHP to produce HTML output to the HTTP response. You should then be using its header() function to set the proper response header. Add the following line to your PHP script before any character is been written to the response.

header("Content-Type: text/html;charset=UTF-8");

See also:

  • PHP UTF-8 cheatsheet
发布评论

评论列表(0)

  1. 暂无评论