jQuery urlencode/decode 补丁帮助
我正在使用这个 jQuery urlencode 和 urldecode 插件 - 非常简单且易于使用,但它并没有以原始形式从字符串中删除 +
。主页上的一条评论建议打一个补丁,但我不知道如何实现它。有人可以帮我吗?
页面:http://www.digitalbart.com/jquery-and-urlencode/
//URL Encode/Decode
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();
var r=/(^[a-zA-Z0-9_.]*)/;
while(x<c.length){var m=r.exec(c.substr(x));
if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
}else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){
b=parseInt(m[1].substr(1),16);
t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});
建议的补丁:
function dummy_url_decode(url) {
// fixed -- + char decodes to space char
var o = url;
var binVal, t, b;
var r = /(%[^%]{2}|\+)/;
while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
if (m[1] == '+') {
t = ' ';
} else {
b = parseInt(m[1].substr(1), 16);
t = String.fromCharCode(b);
}
o = o.replace(m[1], t);
}
return o;
}
谢谢!
I'm using this jQuery urlencode and urldecode plugin - very simple and easy to use but it doesn't, in its original form, remove +
from the string. The one comment on the home page suggests a patch but I don't know how to implement it. Can anyone help me out?
The Page: http://www.digitalbart.com/jquery-and-urlencode/
//URL Encode/Decode
$.extend({URLEncode:function(c){var o='';var x=0;c=c.toString();
var r=/(^[a-zA-Z0-9_.]*)/;
while(x<c.length){var m=r.exec(c.substr(x));
if(m!=null && m.length>1 && m[1]!=''){o+=m[1];x+=m[1].length;
}else{if(c[x]==' ')o+='+';else{var d=c.charCodeAt(x);var h=d.toString(16);
o+='%'+(h.length<2?'0':'')+h.toUpperCase();}x++;}}return o;},
URLDecode:function(s){var o=s;var binVal,t;var r=/(%[^%]{2})/;
while((m=r.exec(o))!=null && m.length>1 && m[1]!=''){
b=parseInt(m[1].substr(1),16);
t=String.fromCharCode(b);o=o.replace(m[1],t);}return o;}
});
The proposed Patch:
function dummy_url_decode(url) {
// fixed -- + char decodes to space char
var o = url;
var binVal, t, b;
var r = /(%[^%]{2}|\+)/;
while ((m = r.exec(o)) != null && m.length > 1 && m[1] != '') {
if (m[1] == '+') {
t = ' ';
} else {
b = parseInt(m[1].substr(1), 16);
t = String.fromCharCode(b);
}
o = o.replace(m[1], t);
}
return o;
}
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根本不要使用该插件;这是毫无意义的。 Javascript 已经支持使用内置函数(
encodeURIComponent
和decodeURIComponent
)进行 URL 编码和解码。使用该机制将空格编码为 %20。如果您的服务器端代码在其中添加加号来表示空格,您可以通过简单的正则表达式替换来删除这些加号,因为不会有任何“真正的”加号需要担心(它们被编码为 %2B):
Don't use that plugin at all; it's pointless. Javascript already supports URL encoding and decoding with built-in functions (
encodeURIComponent
anddecodeURIComponent
). Spaces are encoded as %20 with that mechanism.If your server-side code puts plus signs in there for spaces, you can get rid of those with a simple regex replacement since there won't be any "real" plus signs to worry about (they're encoded as %2B):