How to implement gbk encoding in javascript
javascript实现gbk编码的方法:1、在js建立一个“字符->gbk码”的映射表,通过查表来解决;2、使用escapeDBC和encodeURIComponent进行编码。
本文操作环境:Windows7系统、javascript1.8.5版、Dell G3电脑。
javascript 怎么实现gbk编码?
Javascript对中文GBK编码
今天帮同事弄一个在迅雷新闻上展示的页面,里面的搜索功能对关键词用的是GBK编码,而他们给我的页面上GB2312的,造成搜索功能的关键词乱码。后面google了一下,找到了解决方案,很有效。
以”超级本“这个关键词为例:
GB2312下编码后为%E8%B6%85%E7%BA%A7%E6%9C GBK下编码后为%B3%AC%BC%B6%B1%BE
在 js 中要怎样实现使用gbk集进行 uri 编码呢
%HH 其实就只是把一个字节值转换成2位16进制数字,再在前头加上 % 而己
问题是 js 中没有函数可以支持取得字符的 gbk 编码值 str.charCodeAt(index) 取得的是 unicode 编码值。
现在在网上流行的一种解决方案就是,在 js 建立一个 “字符->gbk码” 的映射表,通过查表来解决
因为字符多,这使得 js 雍肿了不少,而且在网上找到的这些映射表建的是不是全面,很难说。
其实在 ie 中,我们可以借助 VBScript 来支持这个工作。
VBScript 中: (Asc(“盟”) + 65536) Mod 65536 就可以取得字符 “盟” 的 GBK 码 50123
但是其它浏览器不支持 VBScript ,可怎么办?
有这么一个办法:
在页面中插入一个图片 img, 设置 img.src = “…中文…”; 这个时候,浏览器会自动把这个 src 的值进行 uri 编码
而它是使用 gbk 还是 utf8 ,是根据文档编码来决定的.
这时候,我们就可以好好利用一下这个特性:
<meta http-equiv="Content-Type" content="text/html; charset=GBK" /> <script type="text/javascript"> function encodeURL(s) { var img = document.createElement("img"); // escapeDBC 对多字节字符编码的函数 function escapeDBC(s) { if (!s) return "" if (window.ActiveXObject) { // 如果是 ie, 使用 vbscript execScript(‘SetLocale "zh-cn"’, ‘vbscript’); return s.replace(/[\d\D]/g, function($0) { window.vbsval = ""; execScript(‘window.vbsval=Hex(Asc("’ + $0 + ‘"))’, "vbscript"); return "%" + window.vbsval.slice(0,2) + "%" + window.vbsval.slice(-2); }); } // 其它浏览器利用浏览器对请求地址自动编码的特性 img.src = "nothing.action?separator=" + s; return img.src.split("?separator=").pop(); } // 把 多字节字符 与 单字节字符 分开,分别使用 escapeDBC 和 encodeURIComponent 进行编码 return s.replace(/([^\x00-\xff]+)|([\x00-\xff]+)/g, function($0, $1, $2) { return escapeDBC($1) + encodeURIComponent($2||”); }); } alert(encodeURL("中文")); </script>
推荐学习:《javascript基础教程》
The above is the detailed content of How to implement gbk encoding in javascript. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How to use WebSocket and JavaScript to implement an online speech recognition system Introduction: With the continuous development of technology, speech recognition technology has become an important part of the field of artificial intelligence. The online speech recognition system based on WebSocket and JavaScript has the characteristics of low latency, real-time and cross-platform, and has become a widely used solution. This article will introduce how to use WebSocket and JavaScript to implement an online speech recognition system.

WebSocket and JavaScript: Key technologies for realizing real-time monitoring systems Introduction: With the rapid development of Internet technology, real-time monitoring systems have been widely used in various fields. One of the key technologies to achieve real-time monitoring is the combination of WebSocket and JavaScript. This article will introduce the application of WebSocket and JavaScript in real-time monitoring systems, give code examples, and explain their implementation principles in detail. 1. WebSocket technology

Go language encoding analysis: UTF-8 and GBK comparison In the Go language, processing string encoding is one of the common tasks. Among them, UTF-8 and GBK are two commonly used character encoding methods. This article will conduct a detailed comparison between UTF-8 and GBK, discuss their differences and usage, and attach specific code examples. 1. Introduction to UTF-8 and GBK UTF-8: UTF-8 is a variable-length Unicode encoding method that can represent characters in almost all languages in the world. UTF-8

How to use WebSocket and JavaScript to implement an online reservation system. In today's digital era, more and more businesses and services need to provide online reservation functions. It is crucial to implement an efficient and real-time online reservation system. This article will introduce how to use WebSocket and JavaScript to implement an online reservation system, and provide specific code examples. 1. What is WebSocket? WebSocket is a full-duplex method on a single TCP connection.

Introduction to how to use JavaScript and WebSocket to implement a real-time online ordering system: With the popularity of the Internet and the advancement of technology, more and more restaurants have begun to provide online ordering services. In order to implement a real-time online ordering system, we can use JavaScript and WebSocket technology. WebSocket is a full-duplex communication protocol based on the TCP protocol, which can realize real-time two-way communication between the client and the server. In the real-time online ordering system, when the user selects dishes and places an order

JavaScript and WebSocket: Building an efficient real-time weather forecast system Introduction: Today, the accuracy of weather forecasts is of great significance to daily life and decision-making. As technology develops, we can provide more accurate and reliable weather forecasts by obtaining weather data in real time. In this article, we will learn how to use JavaScript and WebSocket technology to build an efficient real-time weather forecast system. This article will demonstrate the implementation process through specific code examples. We

JavaScript tutorial: How to get HTTP status code, specific code examples are required. Preface: In web development, data interaction with the server is often involved. When communicating with the server, we often need to obtain the returned HTTP status code to determine whether the operation is successful, and perform corresponding processing based on different status codes. This article will teach you how to use JavaScript to obtain HTTP status codes and provide some practical code examples. Using XMLHttpRequest

Usage: In JavaScript, the insertBefore() method is used to insert a new node in the DOM tree. This method requires two parameters: the new node to be inserted and the reference node (that is, the node where the new node will be inserted).
