JS Code that implements the server. urlencode and server. urldecode Functions

Source: Internet
Author: User
<SCRIPT>
VaR encodeuri = function (unzipstr, iscusencode ){
If (iscusencode ){
VaR ziparray = new array ();
VaR zipstr = "";
VaR lens = new array ();
For (VAR I = 0; I <unzipstr. length; I ++ ){
VaR AC = unzipstr. charcodeat (I );
Zipstr + = ac;
Lens = Lens. Concat (AC. tostring (). Length );
}
Ziparray = ziparray. Concat (zipstr );
Ziparray = ziparray. Concat (lens. Join ("O "));
Return ziparray. Join ("N ");
} Else {
// Return encodeuri (unzipstr );
VaR zipstr = "";
VaR strspecial = "! "" # $ % & '() * +,/:; <=>? [] ^ '{| }~ % ";
VaR TT = ""; For (VAR I = 0; I <unzipstr. length; I ++ ){
VaR CHR = unzipstr. charat (I );
VaR c = stringtoascii (CHR );
Tt + = CHR + ":" + C + "N ";
If (parseint ("0x" + C)> 0x7f ){
Zipstr + = encodeuri (unzipstr. substr (I, 1 ));
} Else {
If (CHR = "")
Zipstr + = "+ ";
Else if (strspecial. indexof (CHR )! =-1)
Zipstr + = "%" + C. tostring (16 );
Else
Zipstr + = CHR;
}
}
Return zipstr;
}
} VaR decodeuri = function (zipstr, iscusencode ){
If (iscusencode ){
VaR ziparray = zipstr. Split ("N ");
VaR zipsrcstr = ziparray [0];
VaR ziplens;
If (ziparray [1]) {
Ziplens = ziparray [1]. Split ("O ");
} Else {
Ziplens. Length = 0;
}

VaR uzipstr = "";

For (var j = 0; j <ziplens. length; j ++ ){
VaR charlen = parseint (ziplens [J]);
Uzipstr + = string. fromcharcode (zipsrcstr. substr (0, charlen ));
Zipsrcstr = zipsrcstr. Slice (charlen, zipsrcstr. Length );
}
Return uzipstr;
} Else {
// Return decodeuri (zipstr );
VaR uzipstr = ""; For (VAR I = 0; I <zipstr. length; I ++ ){
VaR CHR = zipstr. charat (I );
If (CHR = "+ "){
Uzipstr + = "";
} Else if (CHR = "% "){
VaR ASC = zipstr. substring (I + 1, I + 3 );
If (parseint ("0x" + ASC)> 0x7f ){
Uzipstr + = decodeuri ("%" + ASC. tostring () + zipstr. substring (I + 3, I + 9). tostring ());;
I + = 8;
} Else {
Uzipstr + = asciitostring (parseint ("0x" + ASC ));
I + = 2;
}
} Else {
Uzipstr + = CHR;
}
}
Return uzipstr;
}
} VaR stringtoascii = function (STR ){
Return Str. charcodeat (0). tostring (16 );
} VaR asciitostring = function (asccode ){
Return string. fromcharcode (asccode );
}
</SCRIPT>

AboveCodeThe effect of server. urlencode written in C # is the same. If you need to input Chinese characters on the client, try this function.

Usage: "http://www.fanchuanbook.com/search.aspx? Q = "+ encodeuri (Q, false)

<SCRIPT>

var url = "{phpcms_path} yczx/searchcy. php? Ordertype = 1 & search = 1 & searchtype = content & catid = 183 & KEYWORDS = "+ encodeuri (" {$ title }");
var encode = encodeuri ("{$ title}");
document. write ('');
document. write ('click to view all stages of the venue
');

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.