In ASP (server. urlencode), PHP (urlencode () function encoding results, or through ASP, PHP and other dynamic languages directly write Chinese characters of cookies, when reading with JS, the encoding problem is that the final string is encoded by urlencode, and the data needs to be read from the JS client. In this article, we will talk about how to solve this problem through built-in functions in Js.
I believe all of my friends who have encountered this problem should have some knowledge. Currently, some JS user-defined functions are popular on the Internet to solve this problem, such as VBScript (urldecode ()) and JavaScript (urldecode. These two functions cannot communicate well with ASP (server. urlencode) and PHP (urlencode () functions.
VBScript (function urldecode () and JavaScript (function urldecode () will be reproduced at the end of this article.
The main character in this article is JavaScript (urldecodedecodeuricomponent (). This function name is very common and I really don't know much about it. After all, there are many system functions in JS, which are easy to be omitted. Troubles happened to find this function!
Encoding function: encodeuricomponent ()
Decoding function: decodeuricomponent ()
Decodeuricomponent ()Syntax
Decodeuricomponent (uristring)
Parameter: (uristring) required. A string containing the URI component or other text to be decoded.
Returned value: a copy of uristring. The hexadecimal escape sequence is replaced by the characters they represent.
Instance:
<SCRIPT type = "text/JavaScript"> var test1 = "annoyance"; var Test2 = "% E7 % 83% a6 % E6 % 81% BC"; document. write ("encoding (original =" + test1 + "):" + encodeuricomponent (test1) + "<br/>"); document. write ("decoding (original =" + Test2 + "):" + decodeuricomponent (Test2); </SCRIPT>
Result:
Encoding (original = annoyance): % E7 % 83% a6 % E6 % 81% BC
Decoding (original = % E7 % 83% a6 % E6 % 81% BC): annoyance
Note: This article is only tested in a UTF-8 encoding environment. Because in different encoding environments, after ASP (server. urlencode) is compiledCodeIt seems different. It is to be tested!
======================================
Attachment reprinted:
VBScript (function urldecode ())
<SCRIPT type = "text/VBScript"> <! -- Function urldecode (enstr) dim destr, strspecialdim C, I, vdestr = "" strspecial = "! "" # $ % & '() * +,.-_/:; <=>? @ [\] ^ '{| }~ % "For I = 1 to Len (enstr) C = mid (enstr, I, 1) If C =" % "thenv = eval (" & H "+ mid (enstr, I + 1, 2) If instr (strspecial, CHR (V)> 0 thendestr = destr & CHR (V) I = I + 2 elsev = eval ("& H" + mid (enstr, I + 1, 2) + mid (enstr, I + 4, 2) destr = destr & CHR (V) I = I + 5end ifelseif c = "+" thendestr = destr & "elsedestr = destr & Cend ifend ifnexturldecode = destrend function // --> </SCRIPT>
JavaScript (function urldecode () uses VBScript in a soft manner. It seems that in the Javascript environment, conversions related to ASC, Hex, and CHR, such as Str. charcodeat (0 ). tostring (16) and string. the fromcharcode (STR) encoding results for different Chinese characters are not uniform.
For example, VBScript str2asc/asc2str
<SCRIPT type = "text/VBScript"> function str2asc (strstr) str2asc = hex (ASC (strstr) end function asc2str (ascasc) asc2str = CHR (ascasc) end function msgbox str2asc ("A") msgbox asc2str ("& H61") 'convert 61 to 10 in hexadecimal notation to 97 </SCRIPT>
Javascript str2asc/asc2str
<SCRIPT type = "text/JavaScript"> function str2asc (STR) {return Str. charcodeat (0 ). tostring (16);} function asc2str (STR) {return string. fromcharcode (STR);} alert (str2asc ("A"); // alert (asc2str ("0x61"); // </SCRIPT>
Demo:
<SCRIPT type = "text/VBScript"> function str2asc (strstr) str2asc = hex (ASC (strstr) end function asc2str (ascasc) asc2str = CHR (ascasc) end function </SCRIPT> <SCRIPT type = "text/JavaScript">/* urlencode and urldecode <a href = "/? Tag = % E5 % 87% BD % E6 % 95% B0 "target =" _ blank "> function </a> */function urlencode (STR) {var ret = ""; vaR strspecial = "! \ "# $ % & '() * +,/:; <=>? [] ^ '{| }~ % "; Var TT =" "; for (VAR I = 0; I <Str. length; I ++) {var CHR = Str. charat (I); var c = str2asc (CHR); TT + = CHR + ":" + C + "N"; if (parseint ("0x" + C)> 0x7f) {RET + = "%" + C. slice (0, 2) + "%" + C. slice (-2);} else {If (CHR = "") RET + = "+"; else if (strspecial. indexof (CHR )! =-1) RET + = "%" + C. tostring (16); else RET + = CHR;} return ret;} function urldecode (STR) {var ret = ""; for (VAR I = 0; I <Str. length; I ++) {var CHR = Str. charat (I); If (CHR = "+") {RET + = "";} else if (CHR = "%") {var ASC = Str. substring (I + 1, I + 3); If (parseint ("0x" + ASC)> 0x7f) {RET + = asc2str (parseint ("0x" + ASC + Str. substring (I + 4, I + 6); I + = 5;} else {RET + = asc2str (parseint ("0x" + ASC )); I + = 2 ;}}else {RET + = CHR ;}return RET ;}alert (urldecode ("% C2 % D2 % C2 % Eb ")); </SCRIPT>