How to set the "Content-Type ... charset" in the request header using a HTML link

HAL 9000 picture HAL 9000 · Jun 3, 2011 · Viewed 119.9k times · Source

I have a simple HTML-page with a UTF-8 encoded link.

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <a charset='UTF-8' href='http://server/search?q=%C3%BC'>search for "ü"</a>
  </body>
</html>

However, I don't get the browser to include Content-Type:application/x-www-form-urlencoded; charset=utf-8 into the request header. Therefore I have to configure the webserver to assume all requests are UTF-8 encoded (URIEncoding="UTF-8" in Tomcat server.xml). But of course the admin won't let me do that in the production environment (Websphere).

I know it's quite easy to achieve using Ajax, but how can I control the request header when using standard HTML links? The charset attribute doesn't seem to work for me (tested in IE8 and FF 3.5)

The 2nd part of the required solution would be to set the URL encoding when changing an IFrame's document.location using Javascript.

Answer

BalusC picture BalusC · Jun 3, 2011

This is not possible from HTML on. The closest what you can get is the accept-charset attribute of the <form>. Only MSIE browser adheres that, but even then it is doing it wrong (e.g. CP1252 is actually been used when it says that it has sent ISO-8859-1). Other browsers are fully ignoring it and they are using the charset as specified in the Content-Type header of the response. Setting the character encoding right is basically fully the responsiblity of the server side. The client side should just send it back in the same charset as the server has sent the response in.

To the point, you should really configure the character encoding stuff entirely from the server side on. To overcome the inability to edit URIEncoding attribute, someone here on SO wrote a (complex) filter: Detect the URI encoding automatically in Tomcat. You may find it useful as well (note: I haven't tested it).


Update: Noted should be that the meta tag as given in your question is ignored when the content is been transferred over HTTP. Instead, the HTTP response Content-Type header will be used to determine the content type and character encoding. You can determine the HTTP header with for example Firebug, in the Net panel.

alt text