The ability to implement pinyin and UrlEncode in JS (using GB and Unicode tables)

Source: Internet
Author: User
Tags comparison table implement return urlencode
encode|js| Pinyin

Often written in VB Urlencoding is easy to achieve urlencode, as well as the use
http://www.csdn.net/Develop/read_article.asp?id=13846
The code in can easily be rewritten as a VBS to achieve the pinyin of some Chinese characters

But because some people need to write completely in JavaScript, JavaScript defaults to Unicode
So you need a Unicode and GB conversion library.

Unicode and GB conversion libraries and UrlEncode and Getspell functions, please download
Http://www.blueidea.com/user/qswh/qswhU2GB.js

var qswhu2gb=[];//unicode and GB of corresponding tables, slightly
var qswhspell=[];//pinyin comparison table, slightly

function urlencode (str) {
 var i,c,ret= ", strspecial="!\ "#$%&" () *+,/:;<= >?@[\]^ ' {|} ~%";
 for (i=0;i<str.length;i++) {
  if (str.charcodeat (i) >=0x4e00) {
   c=qswhu2gb[ Str.charcodeat (i) -0x4e00];
   ret+= "%" +c.slice (0,2) + "%" +c.slice (-2);
 }
  else{
   C=str.charat (i);
   if (c== "")
    ret+= "+";
   Else if (Strspecial.indexof (c)!=-1)
    ret+= "%" +str.charcodeat (i). ToString (a);
   else
    ret+=c;
 }
 
 return ret;
}

function Getspell (STR,SP) {
var i,c,t,ret= "";
if (sp==null) sp= "";
for (i=0;i<str.length;i++) {
if (Str.charcodeat (i) >=0x4e00) {
C=parseint (Qswhu2gb[str.charcodeat (i) -0x4e00],16);
if (c<55290) {
for (t=qswhspell.length-1;t>0;t=t-2) if (qswhspell[t]<=c) break;
if (t>0) ret+=qswhspell[t-1]+sp;
}
}
}
Return Ret.substr (0,ret.length-sp.length);
}

The SP is a separator, as the following example
<script Language=javascript src=qswhu2gb.js></script>

<script language=javascript>
document.write (UrlEncode ("Chinese <>\" #%{}|^~[] ' &?+abc ")," <br> ")
document.write (Getspell ("Chinese Programmer's Base"), "<br>"
document.write ("Getspell", "'"), "<br>")
</script>




Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.