In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-31 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Today, I would like to share with you the relevant knowledge about how to achieve gbk coding in javascript. The content is detailed and the logic is clear. I believe most people still know too much about this, so share this article for your reference. I hope you can get something after reading this article. Let's take a look at it.
Javascript to achieve gbk coding methods: 1, in js to establish a "character-> Gbk code" mapping table, by looking up the table to solve; 2, using escapeDBC and encodeURIComponent for coding.
This article operating environment: Windows7 system, javascript1.8.5 version, Dell G3 computer.
How does javascript implement gbk coding?
Javascript encodes Chinese GBK
Today, I helped my colleagues to get a page displayed on Xunlei News, in which the search function used GBK coding for keywords, while they gave me GB2312 on the page, resulting in garbled keywords in the search function. After google for a while, found a solution, very effective.
Take the keyword "ultrabook" as an example:
After encoding in GB2312, it is encoded in% E8%B6%85%E7%BA%A7%E6%9CGBK. It is encoded in% B3%AC%BC%B6%B1%BE.
How to use gbk set to encode uri in js?
% HH is actually just converting a byte value into a 2-digit hexadecimal number, preceded by%.
The problem is that there is no function in js that can support getting the gbk code value of a character str.charCodeAt (index) gets the unicode code value.
Now a popular solution on the Internet is to establish a mapping table of "character-> Gbk code" in js, which can be solved by looking up the table.
Because of the large number of characters, this makes js Yong swollen a lot, and it is hard to say whether these mapping tables found online are comprehensive.
In fact, in ie, we can use VBScript to support this work.
In VBScript: (Asc ("League") + 65536) Mod 65536 can obtain the GBK code 50123 of the character "League".
But other browsers do not support VBScript, what should I do?
There is one way:
Insert a picture img in the page and set img.src = "… Chinese..." At this time, the browser will automatically encode the value of the src with uri
Whether it uses gbk or utf8 depends on the document encoding.
At this point, we can take advantage of this feature:
Function encodeURL (s) {var img = document.createElement ("img"); / / escapeDBC function function escapeDBC (s) {if (! s) return "" if (window.ActiveXObject) {/ / if it is ie, use vbscriptexecScript ('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 (0L2) +"% "+ window.vbsval.slice (- 2);} / / other browsers take advantage of the browser's automatic coding of the request address img.src =" nothing.action?separator= "+ separator= return img.src.split ("? separator= "). Pop () } / / separate multi-byte characters from single-byte characters and use escapeDBC and encodeURIComponent to encode return s.replace (/ ([^\ X00 -\ xff] +) +) | ([\ X00 -\ xff] +) / g, function ($0, $1, $2) {return escapeDBC ($1) + encodeURIComponent ($2 | | ");} alert (encodeURL (" Chinese ")) These are all the contents of the article "how to implement gbk coding in javascript". Thank you for reading! I believe you will gain a lot after reading this article. The editor will update different knowledge for you every day. If you want to learn more knowledge, please pay attention to the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.