简体   繁体   English

Eclipse JavaScript字符编码

[英]Eclipse javascript character encoding

I'd like to display some language specific characters from javascript but I can't. 我想显示来自javascript的一些特定于语言的字符,但不能。

My app is a Java webapp and the front end is jQuery. 我的应用程序是Java webapp,前端是jQuery。 All the characters that are sended from the server - in a JSP or with AJAX - are displayed properly. 从服务器发送的所有字符-在JSP中或通过AJAX-均正确显示。 When I want to display some text hardcoded in to the javascript file it's broken. 当我想显示一些硬编码到javascript文件中的文本时,它已损坏。

I'm using Eclipse. 我正在使用Eclipse。 In the JSP's header I use: 在JSP的标头中,我使用:

<%@ 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. 在Eclipse中,我已经将项目属性/文本文件编码设置为UTF-8,并且我还检查了JS文件的资源属性/文本文件编码也为UTF-8。

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: 奇怪的是:当我尝试一个单独的html文件(在同一项目中)时,它正在工作:

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

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

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

    <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) (我正在使用集成在Eclipse中的Apache Tomcat)

Thanks in advance! 提前致谢!

The c:import uses the server platform default encoding if the encoding is absent in the response header of the request. 如果请求的响应标头中不存在c:import使用服务器平台默认编码。 Rather just use src attribute instead. 而是只使用src属性。 The webbrowser is smarter in this. Web浏览器在这方面更聪明。

<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. 如果确实需要使用c:import (老实说,在这种情况下我看不到任何好处),那么您需要编写一个侦听/JS/*Filter ,它会设置适当的Content-Type标头在响应上使用正确的编码。 This however won't work if it concerns an external URL, it's then the responsibility of the external server. 但是,如果它涉及外部URL,则无法使用,这是外部服务器的责任。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM