有时候可能会有这么一个需求,我在后台使用:
HttpUtility.UrlEncode(str, System.Text.Encoding.UTF8);
将Url进行编码,前台JS需要使用这段内容,这时候就需要解码了:
代码
/**
* Url编码
**/
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解码
**/
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);
}