0

My script is saved in uft-8 encoded file:

test.js

function sayhi(){

    alert('Γειά σου!')

}

In html file (also utf-8) I got :

<html>
  <head>
    <script charset="utf-8"  type="text/javascript" src="../js/mytest.js"></script>
</head>
  <body>
   ...
   <script type="text/javascript">
                alert('Γειά!')
                sayhi();
    <script type="text/javascript">
 </body>
</html>

The problem is that sayhi which is supposed to print utf-8 string - prints an unreadable string as opposed to alert above it (alert('Γειά!')) which prints the correct string.

What am I missing? How to make the sayhi use the correct encoding. Thanks.

PS. The page is served by glassfish app server (not sure if that matters)

Edit 1:

Also in my glassfish-web.xml I have declared

 <parameter-encoding default-charset="UTF-8"/>

Anything else I could do in glasfish to serve javascript correctly?

Edit 2:

I failed to mention that I am also using struts2 and jersey and something tells me that struts 2 is the culprit here. I am thinking that may be an implementation of some filters to change the encoding or a strust directive to ignore the path with javacript may solve the issue... will try the above later.

PKey
  • 3,715
  • 1
  • 14
  • 39
  • 1
    Look at the HTTP Content-Type headers your JS file is served with. – deceze Dec 14 '18 at 11:07
  • have you tried setting the encoding for the html file `` in ``? – Peter Pajchl Dec 14 '18 at 11:12
  • @Peter Pajchi I have already <%@page pageEncoding="UTF-8"%> (a jsp tag which I suppose is enough) any way meta tag did't help – PKey Dec 14 '18 at 11:56
  • @deceze any tip how to do that - I am looking at headers from chrome dev tools ... can't see any Content-Type header though ... – PKey Dec 14 '18 at 12:01
  • 1
    That may be the issue. If your server isn't setting the header, the browser may interpret it in whatever encoding it feels like. – deceze Dec 14 '18 at 12:14
  • @deceze so, if I understand you correctly - this must be application server / glassfish settings issue? – PKey Dec 14 '18 at 12:25
  • That would be my best guess, yes. – deceze Dec 14 '18 at 12:29

2 Answers2

0

You don't actually need a charset for the script tag or the script itself - a charset on the HTML page will suffice. See this answer for more info. All you need to add is this, on your HTML page and in the head:

<meta charset="utf-8 />

And it should work.

Jack Bashford
  • 43,180
  • 11
  • 50
  • 79
  • Since the `alert` within the same page already works, I'd be surprised if this made any difference. – deceze Dec 14 '18 at 11:19
  • I think that the linked answr said that this worked for the external files, but it didn't mention internal fi les not working. – Jack Bashford Dec 14 '18 at 11:23
  • I have already <%@page pageEncoding="UTF-8"%> (a jsp tag which I suppose is enough) any way I tried adding meta tag too - did't help – PKey Dec 14 '18 at 11:59
0

The issue was solved by adding a filter to my web.xml

public class CharsetFilter implements Filter
{
 public static final String DESIRED_ENCODING="UTF-8";
 private String encoding;

 @Override
 public void init(FilterConfig config) throws ServletException
 {
   encoding = config.getInitParameter("appEncoding");
   if( encoding==null ) encoding=DESIRED_ENCODING;
 }

 @Override
 public void doFilter(ServletRequest request, ServletResponse response, FilterChain next)
 throws IOException, ServletException
 {
  //HttpServletRequest rq=(HttpServletRequest)request;  
  //String uri=rq.getRequestURI().toLowerCase();  
  //System.out.println(""+uri);
  //this is used to fix static javascript encoding 
  if (!encoding.equals(response.getCharacterEncoding())){
    response.setCharacterEncoding(encoding);
  }
  if (!encoding.equals(request.getCharacterEncoding())){
    request.setCharacterEncoding(encoding);
  }

  next.doFilter(request, response);
 }

 public void destroy(){}
}

and in web.xml:

        <filter>
        <filter-name>CharsetFilter</filter-name>
        <filter-class>utilities.CharsetFilter</filter-class>
        <init-param>
            <param-name>appEncoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        </filter>
        <filter-mapping>
            <filter-name>CharsetFilter</filter-name>
            <url-pattern>/*</url-pattern>
       </filter-mapping>
PKey
  • 3,715
  • 1
  • 14
  • 39