Home > Web Front-end > Front-end Q&A > How to deal with garbled Chinese characters in JavaScript

How to deal with garbled Chinese characters in JavaScript

藏色散人
Release: 2023-02-03 16:21:14
Original
12140 people have browsed it

Solution to Chinese garbled JavaScript code: 1. Use the "Save As" of the txt text to set the encoding to "utf-8" format; 2. Add the encoding "charset="utf-8" to the js loading code ""; 3. When filtering the full path, use "if (URI.contains(".css") || URI.contains(".js") || URI.contains(".png")) {. ..}" code can be used to judge the file.

How to deal with garbled Chinese characters in JavaScript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, DELL G3 computer.

What should I do if the Chinese javascript is garbled?

JS Chinese garbled code problem

#The fundamental reason why js displays garbled codes in the browser is the encoding problem, so solve it After fixing the encoding problem, js can basically be displayed normally.

1: Use txt text to save as set encoding

In this way, the js file is set to utf-8 encoding.

Two: Add encoding in the js loading code

charset="utf-8"
Copy after login

Three: Special circumstances, filters, general filters rarely filter .js,. png, .css, when filtering the full path, you must judge the file

 public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
        // 请求和响应强转为子类类型
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) resp;
 
        String URI = request.getRequestURI() ;
        //对css js png 文件进行判断,true则直接放行。
        if (URI.contains(".css") || URI.contains(".js") || URI.contains(".png")) {
            chain.doFilter(request, response);
            return ;
        }
        request.setCharacterEncoding("UTF-8");
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html");
        chain.doFilter(request, response);
    }
Copy after login

Register filter:

    <filter>
        <filter-name>CharacterEncodingFilter</filter-name>
        <filter-class>com.huang.filter.CharacterEncodingFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>CharacterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>
Copy after login

After the filter is processed in this way, js, css will not be filtered. The problem is solved

Recommended study: "JavaScript Video Tutorial"

The above is the detailed content of How to deal with garbled Chinese characters in JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template