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 ~