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.
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"
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); }
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>
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!