JavaScript迷惑问题之九:如何对URL进行编码?

Patrick_DK 2002-06-19 12:45:15
如题
...全文
243 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
Go_Rush 2002-08-04
  • 打赏
  • 举报
回复
收藏
java_new 2002-06-24
  • 打赏
  • 举报
回复
构造的url里有中文没有什么关系,在接收的时候直接用request.getParameter("name");能够正确的得到相应的中文
过客猫2022 2002-06-24
  • 打赏
  • 举报
回复
最好不要用%号,会出问题的,可以用$,*,@什么的:),我用的是$
我这里有用ASP写编码函数,是我出的一个测试题答案!我好像在ASP论坛贴出了!
TIYILON 2002-06-24
  • 打赏
  • 举报
回复
qiushuiwuhen(秋水无恨) 兄 真让人崇拜,将底层分析得如此透彻又用javascript 来实现。
Andrawu 2002-06-20
  • 打赏
  • 举报
回复
To qiushuiwuhen(秋水无恨)
研究得这么透,我要好好学学!!!
bistar 2002-06-20
  • 打赏
  • 举报
回复
gz
qiushuiwuhen 2002-06-19
  • 打赏
  • 举报
回复
<script language="vbscript">
function urlencoding(vstrin)
dim i,strreturn
strreturn = ""
for i = 1 to len(vstrin)
thischr = mid(vstrin,i,1)
if abs(asc(thischr)) < &hff then
strreturn = strreturn & thischr
else
innercode = asc(thischr)
if innercode < 0 then
innercode = innercode + &h10000
end if
hight8 = (innercode and &hff00)\ &hff
low8 = innercode and &hff
strreturn = strreturn & "%" & hex(hight8) & "%" & hex(low8)
end if
next
urlencoding = strreturn
end function
</script>

<script>
str="中文Abc"
alert(escape(str))
alert(urlencoding(str))
</script>
lonelyghost 2002-06-19
  • 打赏
  • 举报
回复
函数:escape
例如:
alert(escape("JavaScript迷惑问题之九:如何对URL进行编码?"))
qiushuiwuhen 2002-06-19
  • 打赏
  • 举报
回复
今天又解了版本5.5 encodeURIComponent 的算法,好处
1。低版本可以使用,不存在兼容性问题
2。encodeURIComponent在(55296-57343)范围内不可用

<script language="javascript">
function qswhEncodeURI(str){
/************(qiushuiwuhen 2002-6-19)***************/
var m="",sp="!'()*-.0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ_abcdefghijklmnopqrstuvwxyz~"
for(var i=0;i<str.length;i++){
if(sp.indexOf(str.charAt(i))!=-1){
m+=str.charAt(i)
}else{
var n=str.charCodeAt(i)
var t="0"+n.toString(8)
if(n>0x7ff)
m+=("%"+(224+parseInt(t.slice(-6,-4),8)).toString(16)+"%"+(128+parseInt(t.slice(-4,-2),8)).toString(16)+"%"+(128+parseInt(t.slice(-2),8)).toString(16)).toUpperCase()
else if(n>0x7f)
m+=("%"+(192+parseInt(t.slice(-4,-2),8)).toString(16)+"%"+(128+parseInt(t.slice(-2),8)).toString(16)).toUpperCase()
else if(n>0x3f)
m+=("%"+(64+parseInt(t.slice(-2),8)).toString(16)).toUpperCase()
else if(n>0xf)
m+=("%"+n.toString(16)).toUpperCase()
else
m+=("%"+"0"+n.toString(16)).toUpperCase()
}
}
return m;
}
var s=55000,iCount=0,iError=0;
for(var i=0;i<3000;i++){
str=String.fromCharCode(s+i)
try{if(qswhEncodeURI(str)==encodeURIComponent(str))iCount++;}catch(e){document.write("<br>",++iError,":",s+i,"=",qswhEncodeURI(str))}
}
alert(iCount)
</script>
qiushuiwuhen 2002-06-19
  • 打赏
  • 举报
回复
另外一种检验是否正确的方法就是

<form action=http://www.csdn.net/ method=get>
<input name=demo value='中文 <>""#%{}|^~[]`&?+Abc'>
emu 2002-06-19
  • 打赏
  • 举报
回复
秋水,服务端脚本怎么玩啊,我不会的。
qiushuiwuhen 2002-06-19
  • 打赏
  • 举报
回复
URLEncode实际上做的是5.5的encodeURIComponent
但encodeURIComponent中'和~不算特殊字符,呵呵

<script language="javascript"">
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;
}
</script>

<script language="javascript">
str="中文 <>\"#%{}|^~[]`'&?+Abc";
document.write(URLEncode(str),"<br>");
document.write(encodeURIComponent(str),"<br>");
</script>
qiushuiwuhen 2002-06-19
  • 打赏
  • 举报
回复
to emu:你可以用服务端脚本的urldecode就知道真伪了
qiushuiwuhen 2002-06-19
  • 打赏
  • 举报
回复
但补全了urlencoding,hehe

<script language="vbscript">
function urlencoding(vstrin)
dim i,strreturn,strSpecial
strSpecial = " <>""#%{}|^~[]`'&?+"
strreturn = ""
for i = 1 to len(vstrin)
thischr = mid(vstrin,i,1)
if abs(asc(thischr)) < &hff then
if instr(strSpecial,thischr)>0 then
strreturn = strreturn & "%" & hex(asc(thischr))
else
strreturn = strreturn & thischr
end if
else
innercode = asc(thischr)
if innercode < 0 then
innercode = innercode + &h10000
end if
hight8 = (innercode and &hff00)\ &hff
low8 = innercode and &hff
strreturn = strreturn & "%" & hex(hight8) & "%" & hex(low8)
end if
next
urlencoding = strreturn
end function
alert(urlencoding("中文 <>""#%{}|^~[]`'&?+Abc"))
</script>




emu 2002-06-19
  • 打赏
  • 举报
回复
怎么结果个不相同的?哪个标准啊?

str="中文Abc"
alert(urlencoding(str))
alert(URLEncode(str))

%D6%D0%CE%C4Abc
%E4%B8%AD%E6%96%87Abc
qiushuiwuhen 2002-06-19
  • 打赏
  • 举报
回复
URLEncode 可惜有三
Patrick_DK 2002-06-19
  • 打赏
  • 举报
回复
是这样的,我需要在JS代码块中构造一个带查询参数的url字符串
然后再window.location.reload()这个url
而这url中的一个查询参数name的值是中文的
如果我构造url的时候用了escape()的话
发送和返回的都是一串Name=%u4E13%u4E1A%u5206%u7C7B这样的编码
我想知道JavaScript中有没有什么办法
可以实现Java中的java.net.URLEncoder.encode()方法呢

87,996

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 JavaScript
社区管理员
  • JavaScript
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧