views:

134

answers:

1

Hi,

I'd like to display some language specific characters from javascript but I can't.

My app is a Java webapp and the front end is jQuery. All the characters that are sended from the server - in a JSP or with AJAX - are displayed properly. When I want to display some text hardcoded in to the javascript file it's broken.

I'm using Eclipse. In the JSP's header I use:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

I've tried this (charset in the script element) too:

<script type="text/javascript" charset="utf-8"><c:import url="/JS/mainJS.js" /></script>

In my Eclipse I've set the project properties / text file encoding to UTF-8 AND I've checked the JS files' resource properties / text file encoding that is UTF-8 too.

But when I try this:

$.test = function(){
    var s = "éééáááűűűű";
    alert(s);
}

I get:

éééáááűűűű

The strange thing is that: When I try in a separate html file (in the same project), It's working:

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;

<html>
  <head>
    <title>Test</title>

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"&gt;&lt;/script&gt;

    <style type="text/css">
        body {background: #c0c0c0;}
    </style>

    <script type="text/javascript">
        $(document).ready(function(){
            var s = "éááűűúúú";
            $("#console").text(s);
            alert(s);
        })
    </script>

  </head>

  <body>
        <div id="console"></div>
  </body>
</html>

Even if I DO NOT use any content type and page encoding settings.

What is the problem? What shall I do? (I'm using Apache Tomcat integrated in Eclipse)

Thanks in advance!

+1  A: 

The c:import uses the server platform default encoding if the encoding is absent in the response header of the request. Rather just use src attribute instead. The webbrowser is smarter in this.

<script type="text/javascript" src="/JS/mainJS.js"></script>

(if necessary remove the leading slash if you're running on a fixed context instead of on domain root)

If you really, really need the c:import for this (for which I honestly don't see any advantage in this particular situation), then you need to write a Filter listening on /JS/* which sets the appropriate Content-Type header with the correct encoding on the response. This however won't work if it concerns an external URL, it's then the responsibility of the external server.

BalusC
Yeah, you're right! Thank you very much!
Bardock
You're welcome. Try not to overcomplicate things ;)
BalusC