这是我收藏的一段代码,我自己没有验证过代码,你可以参考一下:function URLEncode(strURL) { var strSpecialUrl = " <>\"#%{}|^~[]`'&?+"; var strEncode=""; var i, j, chUrl, iCode, iCodeBin, num; var tempBin; var leadingzeros; strURL+=""; for (i=0; i<strURL.length; i++) { chUrl = strURL.charAt(i); iCode = chUrl.charCodeAt(0); if (iCode<=parseInt("0x7F")) { if (strSpecialUrl.indexOf(chUrl)!=-1) { //chUrl is a special character that needs to be Url encoded strEncode+="%"+iCode.toString(16).toUpperCase(); } else { //otherwise chrUrl is normal strEncode+=chUrl; } } else { leadingzeros=""; iCodeBin=iCode.toString(2) if (iCode<=parseInt(0x7FF)) { //glyph is represented by two chars //check leading zeros on iCodeBin (should be 11 digits) for (j=11; j>iCodeBin.length; j--) leadingzeros+="0"; iCodeBin=leadingzeros+iCodeBin tempBin="110"+iCodeBin.substr(0,5); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(5,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() } else { if (iCode<=parseInt(0xFFFF)) { //glyph is represented by three chars //check leading zeros on iCodeBin (should be 16 digits) for (j=16; j>iCodeBin.length; j--) leadingzeros+="0"; iCodeBin=leadingzeros+iCodeBin tempBin="1110"+iCodeBin.substr(0,4); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(4,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(10,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() } else { if (iCode<=parseInt(0x1FFFFF)) { //glyph is represented by four chars //check leading zeros on iCodeBin (should be 21 digits) for (j=21; j>iCodeBin.length; j--) leadingzeros+="0"; iCodeBin=leadingzeros+iCodeBin tempBin="11110"+iCodeBin.substr(0,3); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(3,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(9,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(15,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() } else { if (iCode<=parseInt(0x3FFFFFF)) { //glyph is represented by five chars //check leading zeros on iCodeBin (should be 26 digits) for (j=26; j>iCodeBin.length; j--) leadingzeros+="0"; iCodeBin=leadingzeros+iCodeBin tempBin="111110"+iCodeBin.substr(0,2); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(2,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(8,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(14,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(20,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() } else { if (iCode<=parseInt(0x7FFFFFFF)) { //glyph is represented by six chars //check leading zeros on iCodeBin (should be 31 digits) for (j=31; j>iCodeBin.length; j--) leadingzeros+="0"; iCodeBin=leadingzeros+iCodeBin tempBin="1111110"+iCodeBin.substr(0,1); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(1,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(7,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(13,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(19,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() tempBin="10"+iCodeBin.substr(25,6); strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase() } } } } } } } return strEncode; }
{
var strSpecialUrl = " <>\"#%{}|^~[]`'&?+";
var strEncode="";
var i, j, chUrl, iCode, iCodeBin, num;
var tempBin;
var leadingzeros; strURL+="";
for (i=0; i<strURL.length; i++) {
chUrl = strURL.charAt(i);
iCode = chUrl.charCodeAt(0);
if (iCode<=parseInt("0x7F")) {
if (strSpecialUrl.indexOf(chUrl)!=-1) {
//chUrl is a special character that needs to be Url encoded
strEncode+="%"+iCode.toString(16).toUpperCase();
} else {
//otherwise chrUrl is normal
strEncode+=chUrl;
}
} else {
leadingzeros="";
iCodeBin=iCode.toString(2)
if (iCode<=parseInt(0x7FF)) {
//glyph is represented by two chars //check leading zeros on iCodeBin (should be 11 digits)
for (j=11; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="110"+iCodeBin.substr(0,5);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(5,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
} else {
if (iCode<=parseInt(0xFFFF)) {
//glyph is represented by three chars //check leading zeros on iCodeBin (should be 16 digits)
for (j=16; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="1110"+iCodeBin.substr(0,4);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(4,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(10,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
} else {
if (iCode<=parseInt(0x1FFFFF)) {
//glyph is represented by four chars //check leading zeros on iCodeBin (should be 21 digits)
for (j=21; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="11110"+iCodeBin.substr(0,3);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(3,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(9,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(15,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
} else {
if (iCode<=parseInt(0x3FFFFFF)) {
//glyph is represented by five chars //check leading zeros on iCodeBin (should be 26 digits)
for (j=26; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="111110"+iCodeBin.substr(0,2);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(2,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(8,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(14,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(20,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
} else {
if (iCode<=parseInt(0x7FFFFFFF)) {
//glyph is represented by six chars //check leading zeros on iCodeBin (should be 31 digits)
for (j=31; j>iCodeBin.length; j--) leadingzeros+="0";
iCodeBin=leadingzeros+iCodeBin tempBin="1111110"+iCodeBin.substr(0,1);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(1,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(7,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(13,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(19,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
tempBin="10"+iCodeBin.substr(25,6);
strEncode+="%"+parseInt(tempBin,2).toString(16).toUpperCase()
}
}
}
}
}
}
}
return strEncode;
}
不知道楼主得到这段“%CD%F8%D2%B3%CB%D1%CB%F7”的页面的编码是什么,即以下????处的内容<html>
<head>
<meta http-equiv="Content-Type" content="text/html;charset=????">
<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>New Page 1</title>
</head>
<body>
<form >
<input name=a value="网页搜索">
</form>
</body></html>