Convert string to gb2312 or UTF-8 encoded parameters (for js)

Source: Internet
Author: User

In many cases, when we pass Chinese parameters directly in the url, the Chinese characters we read are garbled characters. How can we convert these parameters?

Next we will introduce the method
1. Create a New UrlEncode. js and copy the following code.Copy codeThe Code is as follows: // JS Server. UrlEncode encoding function
String. prototype. UrlEncodeGB2312 = function (){
Var str = this;
Str = str. replace (/./g, function (sHex ){
Window. EnCodeStr = "";
Window. sHex = sHex;
Window.exe cScript ('window. EnCodeStr = Hex (Asc (window. sHex) ', "vbscript ");
Return window. EnCodeStr. replace (/../g, "% {blogcontent} amp ;");
});
Return str;
}
String. prototype. UrlEncode = function (){
Var s = escape (this );
Var sa = s. split ("% ");
Var retV = "", retE = "";
If (sa [0]! = ""){
RetV = sa [0];
}
For (var I = 1; I <sa. length; I ++ ){
If (sa [I]. substring (0, 1) = "u "){
RetV + = Hex2Utf8 (Str2Hex (sa [I]. substring (1, 5 )));
If (sa [I]. length> 4)
RetV + = sa [I]. substring (5 );
}
Else retV + = "%" + sa [I];
}
Return retV;
}
Function Str2Hex (s ){
Var c = "";
Var n;
Var ss = "0123456789 ABCDEF ";
Var digS = "";
For (var I = 0; I <s. length; I ++ ){
C = s. charAt (I );
N = ss. indexOf (c );
DigS + = Dec2Dig (eval (n ));
}
Return digS;
}
Function Dec2Dig (n1 ){
Var s = "";
Var n2 = 0;
For (var I = 0; I <4; I ++ ){
N2 = Math. pow (2, 3-I );
If (n1> = n2 ){
S + = '1 ';
N1 = n1-n2;
}
Else
S + = '0 ';
}
Return s;
}
Function Dig2Dec (s ){
Var retV = 0;
If (s. length = 4 ){
For (var I = 0; I <4; I ++ ){
RetV + = eval (s. charAt (I) * Math. pow (2, 3-I );
}
Return retV;
}
Return-1;
}
Function Hex2Utf8 (s ){
Var retS = "";
Var tempS = "";
Var ss = "";
If (s. length = 16 ){
TempS = "1110" + s. substring (0, 4 );
TempS + = "10" + s. substring (4, 10 );
TempS + = "10" + s. substring (10, 16 );
Var sss = "0123456789 ABCDEF ";
For (var I = 0; I <3; I ++ ){
RetS + = "% ";
Ss = tempS. substring (I * 8, (eval (I) + 1) * 8 );
RetS + = sss. charAt (Dig2Dec (ss. substring (0, 4 )));
RetS + = sss. charAt (Dig2Dec (ss. substring (4, 8 )));
}
Return retS;
}
Return "";
}

2. The usage is of course our (string. urlEncode () to convert a string to a url parameter encoded in UTF-8 (string. urlEncodeGB2312 () can be used to convert a string to a gb2312 encoded parameter. Well, O (encoding _ encoding) O Haha ~

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.