On the ASP (Server.URLEncode), PHP (UrlEncode ()) function encoding results, or through the ASP, PHP and other dynamic languages directly to write cookies in the Chinese characters, with JS Read, will encounter a coding problem, That is, the final string is urlencode encoded, and there is a need to read the data from JS on the client.
And this article, I would like to talk about how in JS through the system with the function to solve this problem.
And believe that the friends who have encountered this problem should have some understanding, the current network popular some JS under the custom function to solve this problem, such as VBScript (UrlDecode ()), JavaScript (UrlDecode ()) and so on. Neither of these functions can communicate well with the ASP (Server.URLEncode), PHP (UrlEncode ()) functions.
About VBScript (function UrlDecode ()), JavaScript (function UrlDecode ()) is reproduced at the end of this article.
The main character of this article is JavaScript (Urldecodedecodeuricomponent ()), the function name is too often, the individual really do not understand, after all, JS system functions are many, it is easy to miss. Trouble found this function by accident!
Encoding function: encodeURIComponent ()
decoding function: decodeURIComponent ()
decodeURIComponent () syntax
Copy Code code as follows:
decodeURIComponent (uristring)
Parameters: (uristring) required. A string that contains the encoded URI component or other text to decode.
Return value: A copy of the uristring in which the hexadecimal escape sequence is replaced by the character they represent.
Instance:
Copy Code code as follows:
<script type= "Text/javascript" >
var test1= "annoyance";
var test2= "%E7%83%A6%E6%81%BC";
document.write ("code (formerly =" +test1+ "):" +encodeuricomponent (test1) + "<br/>");
document.write ("Decoding (formerly =" +test2+ "):" +decodeuricomponent (test2));
</script>
Results:
Copy Code code as follows:
Coding (original = annoyance):%E7%83%A6%E6%81%BC
Decoding (original =%E7%83%A6%E6%81%BC): Annoyance
Note: This article is only tested in the UTF-8 coding environment. Because in the different coding environment, the ASP (Server.URLEncode) compiles the code to appear to be different, waits for the test!
Attached reprint:
VBScript (function UrlDecode ())
Copy Code code as follows:
<script type= "Text/vbscript" >
<!--
Function UrlDecode (ENSTR)
Dim destr,strspecial
Dim c,i,v
Destr= ""
Strspecial= "!" " #$%& ' () *+,.-_/:;<=>?@[\]^ ' {|} ~%"
For I=1 to Len (ENSTR)
C=mid (enstr,i,1)
If c= "%" then
V=eval ("&h" +mid (enstr,i+1,2))
If InStr (STRSPECIAL,CHR (v)) >0 Then
DESTR=DESTR&CHR (v)
I=i+2
Else
V=eval ("&h" + Mid (enstr,i+1,2) + mid (enstr,i+4,2))
Destr=destr & Chr (v)
I=i+5
End If
Else
If c= "+" then
destr=destr& ""
Else
Destr=destr&c
End If
End If
Next
Urldecode=destr
End Function
-->
</script>
JavaScript (function UrlDecode ()) is actually a soft use of VBScript, as if in a JavaScript environment, for ASC, Hex, CHR-related transformations, such as Str.charcodeat (0). The results of toString (16) and String.fromCharCode (str) are not uniform for Chinese encoding under different coding.
For example: VBScript Str2asc/asc2str
Copy Code code as follows:
<script type= "Text/vbscript" >
Function STR2ASC (STRSTR)
STR2ASC = Hex (ASC (STRSTR))
End Function
Function Asc2str (ASCASC)
ASC2STR = Chr (ASCASC)
End Function
MsgBox STR2ASC ("a")
MsgBox asc2str ("&h61") ' 16 rpm 61 to 10 is 97.
</script>
JavaScript str2asc/asc2str
Copy Code code as follows:
<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>
Demonstrate:
<script type= "Text/vbscript" > Function str2asc (strstr) STR2ASC = Hex (ASC (STRSTR)) End Function Function Asc2str (ASCASC) asc2str = Chr (ASCASC) End Function &L t;/script> <script type= "Text/javascript" >/* Here we start UrlEncode and UrlDecode functions/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>
[Ctrl + A All SELECT Note: If the need to introduce external JS need to refresh to perform]