jQuery urlencode/decode 补丁帮助

发布于 2024-08-27 04:33:31 字数 1328 浏览 11 评论 0原文

我正在使用这个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

风柔一江水 2024-09-03 04:33:32

根本不要使用该插件;这是毫无意义的。 Javascript 已经支持使用内置函数(encodeURIComponentdecodeURIComponent)进行 URL 编码和解码。使用该机制将空格编码为 %20。

如果您的服务器端代码在其中添加加号来表示空格,您可以通过简单的正则表达式替换来删除这些加号,因为不会有任何“真正的”加号需要担心(它们被编码为 %2B):

var decoded = decodeURIComponent(encoded.replace(/\+/g, '%20'));

Don't use that plugin at all; it's pointless. Javascript already supports URL encoding and decoding with built-in functions (encodeURIComponent and decodeURIComponent). 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):

var decoded = decodeURIComponent(encoded.replace(/\+/g, '%20'));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文