Home  >  Article  >  Web Front-end  >  Javascript UrlDecode function code_javascript skills

Javascript UrlDecode function code_javascript skills

WBOY
WBOYOriginal
2016-05-16 18:36:541452browse

Sometimes there may be such a requirement, I use it in the background:

Copy code The code is as follows:

HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8);

Encode the Url. The front-end JS needs to use this content. At this time, it needs to be decoded:

Code
Copy code The code is as follows:

/**
* Url encoding
**/
ShengUtils.encode = function(unzipStr){
var zipstr="";
var strSpecial="!"#$%&'()* ,/:;<=>?[]^ `{|}~%";
var tt= "";
for(var i=0;ivar chr = unzipStr.charAt(i);
var c=ShengUtils.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;
}

/**
* Url decoding
**/
ShengUtils.decode=function(zipStr){
var uzipStr="";
for(var i=0;ivar 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 =ShengUtils.AsciiToString(parseInt("0x" asc));
i = 2;
}
}else{
uzipStr = chr;
}
}
return uzipStr;
}

ShengUtils.StringToAscii=function(str ){
return str.charCodeAt(0).toString(16);
}

ShengUtils.AsciiToString = function(asccode){
return String.fromCharCode(asccode);
}
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn