函数:escape
例如:
alert(escape("JavaScript迷惑问题之九:如何对URL进行编码?"))
例如:
alert(escape("JavaScript迷惑问题之九:如何对URL进行编码?"))
解决方案 »
- [css]高手进来 object.style.backgroundPosition只改变一个参数的值?
- 页面停止js
- js报出参数无效错误
- 我在Dtree中添加了复选框,我想遍历整棵树选出由复选框中选择的节点。并且把所有节点的名字赋值到一个网页的文本框中。
- js 关于方向键和table
- 打家帮忙看一下
- js获取下拉列表框的长度
- ●●●JS限制上传文件大小的问题:为什么取得的fileSize总是-1??●●●
- 关于innerHTML如更改其中的ID?
- 如何对url进行utf-8编码?
- cookie的值为何取不到?
- 谢谢 meizz(梅花雨),evonne_feng(evonne)等大虾帮助!!!
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>
然后再window.location.reload()这个url
而这url中的一个查询参数name的值是中文的
如果我构造url的时候用了escape()的话
发送和返回的都是一串Name=%u4E13%u4E1A%u5206%u7C7B这样的编码
我想知道JavaScript中有没有什么办法
可以实现Java中的java.net.URLEncoder.encode()方法呢
alert(urlencoding(str))
alert(URLEncode(str))%D6%D0%CE%C4Abc
%E4%B8%AD%E6%96%87Abc
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>
但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>
<input name=demo value='中文 <>""#%{}|^~[]`&?+Abc'>
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>
研究得这么透,我要好好学学!!!
我这里有用ASP写编码函数,是我出的一个测试题答案!我好像在ASP论坛贴出了!