Skip to content Skip to sidebar Skip to footer

Where To Add The Utf-8 Extension In The Html Page?

I need to add the charset='utf-8' at the end of the script tags to get the translation to another language done. I don know where all I should add the tags. Any rules are followed

Solution 1:

If a Content-Type header is present in the HTTP response headers, then this will override the meta headers. Very often, this header is already by default supplied by the webserver and more than often the charset is absent (which would assume client's default charset which is often ISO-8859-1). In other words, the meta headers are generally only interpreted whenever the resources are opened locally (not by HTTP). Big chance that this is the reason that your meta headers apparently didn't work when served over HTTP.

You can use Firebug or Fiddler2 to determine the HTTP response headers. Below is a Firebug screen:

alt text

You can configure the general setting for HTTP response headers at webserver level. You can also configure it on a request basis at programming language level. Since it's unclear what webserver / programming language you're using, I can't go in detail about how to configure it accordingly.


Update: as per the problem symptoms, which is the following typical MySQL exception:

java.sql.SQLException: Incorrect stringvalue: '\xD8\xB3\xD9\x84\xD8\xA8...'

The byte sequence D8 B3 D9 84 D8 A8 is a valid UTF-8 sequence which represents those characters سلب (U+0633, U+0644 and U+0628). So the HTTP part is fine. You mentioned that you were using Jetty 6 as servletcontainer. The later builds of Jetty 6 already support UTF-8 out of the box.

However, the problem is in the DB part. This exception indicates that the charset the DB/table is been instructed to use doesn't support this byte sequence. This can happen when the DB/table isn't been instructed to use UTF-8.

To fix the DB part, issue those MySQL commands:

ALTER DATABASE db_name DEFAULTCHARACTERSET utf8 COLLATE utf8_general_ci;
ALTERTABLE table_name CONVERTTOCHARACTERSET utf8 COLLATE utf8_general_ci;

And for future DB/tables, use CHARACTER SET utf8 COLLATE utf8_general_ci in CREATE statement as well.

Solution 2:

It is usually enough to declare the main document's encoding once, either through a content-type header (See @BalusC's answer for an in-depth explanation) or, if that is not available, the Meta tag.

If you use the Meta tag, make sure it is in the first line of the head section.

There should be no need to specify the character set explicitly for the script files.

Of course, all the content you deal with needs to be UTF-8 encoded as well for this to work. It's not enough to just slap the content-type meta tag in front. (But you are probably aware of that.)

Solution 3:

In this way:

<scripttype="text/javascript"src="[path]/myscript.js"charset="utf-8"></script>

Post a Comment for "Where To Add The Utf-8 Extension In The Html Page?"