encodeURIComponent()对空格的处理与x-www-form-urlencoded标准的 不同。根据x-www-form- urlencoded标准,空格应编码为+。和换行符 应编码为%0D%0A。 JavaScript encodeURIComponent函数将空格编码为%20。 在Windows操作系统上,新行被编码为%0D%0A。但是在Mac OS X上,新行被编码为%0A。我不知道 Linux。有人可以在表单中使用textarea进行检查吗? 某些开发人员可能需要使用application / x-www-form-urlencoded 格式。我们可以包装encodeURIComponent。 if(typeof encodeURIComponent!=''undefined''&& String.prototype.replace){ var urlEncode = function(s){ 返回encodeURIComponent(s).replace(/%20 /,''+'')。replace(/(。 {0,3})(%0A)/ g, 函数(m,p1,p2){return p1 +(p1 ==''%0D'' ?'''':''%0D'')+ p2;}); }; })(); } String.prototype.replace的功能检测是不够的 因为我们真的需要知道替换的第二个参数是否可以 是一个功能。我想我们可以通过使用try-catch和 来测试这个实际上使用replace作为第二个参数的函数。 有人知道如何在不使用try-catch的情况下进行检查吗? 对于具有服务器端工具包能够处理 输出的开发人员encodeURIComponent的正则表达式工作不是必需的,可以使用这个简单的小函数来支持。 if(typeof encodeURIComponent!=''undefined''){ var urlEncode = function(s){ return encodeURIComponent(s); }; } 甚至可能有一个没有textareas的表格版本所以只需要 空格需要更换。 if(typeof encodeURIComponent!=''undefined''&& String.prototype.replace){ var urlEncode = function(s ){ 返回encodeURIComponent(s).replace(/%20 /,''+''); }; }) (); } -------------- BTW,我为代码设置了svn / trac并发布了getOptionVa lue代码 由表格序列化的前一个帖子产生。 < URL:http://cljs.michaux.ca/trac/browser/ trunk / src / getOptionValue> 评论权限,请给我发电子邮件。如果你有一个用户名/密码,请告诉我 首选项。 彼得 解决方案 Peter Michaux写道: 在Windows操作系统上,新行被编码为%0D%0A。但是在Mac OS X上,新行被编码为%0A。我不知道 Linux。有人可以在表单中使用textarea进行检查吗? 与Windows相同:%0D%0A之后 Linux xxxx 2.6 .17-12-generic#2 SMP Sun Sep 23 22:56:28 UTC 2007 i686 GNU / Linux Peter Michaux写道: JavaScript encodeURIComponent函数将空格编码为%20。 在Windows操作系统上,新行编码为 %0D 0A%"但是在Mac OS X上,新行被编码为%0A。我不知道 Linux。有人可以在表单中使用textarea进行检查吗? 是但是%0A只是因为Mac OSX是UNIX衍生物( 历来总是使用%0A)。 Mac直到OS9应该返回%0D。 http://en.wikipedia.org/wiki/Line_Feed %0A:Multics,Unix和类Unix系统(GNU / Linux,AIX ,Xenix,Mac OS X等),BeOS,Amiga,RISC OS等 %0D%0A:DEC RT-11和大多数其他早期非-Unix,非IBM操作系统,CP / M, MP / M,MS-DOS,OS / 2,Microsoft Windows %0D:Commodore机器,Apple II家庭和Mac OS最高版本9 - 巴特 11月21日,上午1:44,Bart Van der Donck< b ... @nijlen.comwrote: Peter Michaux写道: JavaScript encodeURIComponent函数将空格编码为%2 0> 在Windows操作系统上,新行被编码为%0D%0A。但是在Mac OS X上,新行被编码为%0A。我不知道 Linux。有人可以在表单中使用textarea进行检查吗? 是但是%0A仅仅是因为Mac OSX是UNIX衍生物( 历史上总是使用%0A)。 Mac直到OS9应该返回%0D。 http://en.wikipedia.org/wiki/Line_Feed %0A:Multics,Unix和类Unix系统(GNU / Linux,AIX ,Xenix,Mac OS X等),BeOS,Amiga,RISC OS等 %0D%0A:DEC RT-11和大多数其他早期非-Unix,非IBM操作系统,CP / M, MP / M,MS-DOS,OS / 2,Microsoft Windows %0D:Commodore机器,Apple II家庭和Mac OS最高版本9 感谢pr和Bart。似乎需要更正两个选项('%0D''和''%0A'') 。怎么样? var urlencode =(function(){ var f = function(s){ 返回encodeURIComponent(s).replace(/%20 /, ''+'')。replace(/(。{0,3})(%0A)/ g, 函数(m,p1,p2){return p1 +(p1 ==''%0D''?'''':''%0D'')+ p2;} ).replace(/(%0D)(。{0,3})/ g, 函数(m,p1,p2){return p1 +(p2 ==''%0A' '?'''':''%0A'')+ p2;}); }; 试试{ if(f(''\ n \ r'')==''%0D%0A +%0D%0A''){ 返回f; } } catch(e){} })(); 使用try-catch是避免主机对象功能测试的好方法,并且确保正则表达式的所有必要部分都符合预期的b / b 。这样就可以在非常小的空间内对新功能进行全面测试。 但是,try-catch并不会吸引一些程序员。测试替换的任何其他 选项可以用于第二个 参数的函数吗? Peter encodeURIComponent is commonly used to serialize forms for use withXMLHttpRequest requests. For a perfect simulation of browser formrequests, the goal is to serialize form data in the <URL:http://www.w3.org/TR/1999/REC-html401-19991224/interact/forms.html#h-17.13.4.1>application/x-www-form-urlencoded</astandardized format.The handling of whitespace by encodeURIComponent() is different fromthe x-www-form-urlencoded standard. According to the x-www-form-urlencoded standard, a space should be encoded as a "+" and a newlineshould be encoded as a "%0D%0A".The JavaScript encodeURIComponent function encodes a space as "%20".On Windows operating system a new line is encoded as "%0D%0A" but onMac OS X a new line is encoded as just "%0A". I don''t know aboutLinux. Can someone check using a textarea in a form?Some developers may need to have the application/x-www-form-urlencodedformat. We can wrap encodeURIComponent.if (typeof encodeURIComponent != ''undefined'' &&String.prototype.replace) {var urlEncode = function(s) {return encodeURIComponent(s).replace(/%20/, ''+'').replace(/(.{0,3})(%0A)/g,function(m, p1, p2) {return p1+(p1==''%0D''?'''':''%0D'')+p2;});};})();}The feature detection for String.prototype.replace is not sufficientbecause we really need to know if the second argument to replace canbe a function. I imagine we can test this by using try-catch andactually using replace with a function as the second argument. Doesanyone know a way to check without using try-catch?For developers with server-side toolkits capable of dealing with theoutput of encodeURIComponent the regexp work is not necessary and canuse just this simple little function.if (typeof encodeURIComponent != ''undefined'') {var urlEncode = function(s) {return encodeURIComponent(s);};}There could even be a version for forms without textareas so only thespaces need replacement.if (typeof encodeURIComponent != ''undefined'' &&String.prototype.replace) {var urlEncode = function(s) {return encodeURIComponent(s).replace(/%20/, ''+'');};})();}--------------BTW, I set up svn/trac for the code and posted the getOptionValue codethat resulted from the previous thread on form serialization.<URL: http://cljs.michaux.ca/trac/browser/trunk/src/getOptionValue>Please email me if you would like wiki editing and ticket creation/commenting permissions. Let me know if you have a username/passwordpreference.Peter 解决方案 Peter Michaux wrote:On Windows operating system a new line is encoded as "%0D%0A" but onMac OS X a new line is encoded as just "%0A". I don''t know aboutLinux. Can someone check using a textarea in a form?Same as Windows: before%0D%0AafterLinux xxxx 2.6.17-12-generic #2 SMP Sun Sep 23 22:56:28 UTC 2007 i686GNU/LinuxPeter Michaux wrote:The JavaScript encodeURIComponent function encodes a space as "%20".On Windows operating system a new line is encoded as "%0D%0A" but onMac OS X a new line is encoded as just "%0A". I don''t know aboutLinux. Can someone check using a textarea in a form?Yes but %0A is only because Mac OSX is a UNIX derivate (whichhistorically always used %0A).Mac until OS9 should return %0D. http://en.wikipedia.org/wiki/Line_Feed%0A: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, MacOS X, etc.), BeOS, Amiga, RISC OS, and others%0D%0A: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M,MP/M, MS-DOS, OS/2, Microsoft Windows%0D: Commodore machines, Apple II family and Mac OS up to version 9--BartOn Nov 21, 1:44 am, Bart Van der Donck <b...@nijlen.comwrote:Peter Michaux wrote: The JavaScript encodeURIComponent function encodes a space as "%20". On Windows operating system a new line is encoded as "%0D%0A" but on Mac OS X a new line is encoded as just "%0A". I don''t know about Linux. Can someone check using a textarea in a form?Yes but %0A is only because Mac OSX is a UNIX derivate (whichhistorically always used %0A).Mac until OS9 should return %0D. http://en.wikipedia.org/wiki/Line_Feed%0A: Multics, Unix and Unix-like systems (GNU/Linux, AIX, Xenix, MacOS X, etc.), BeOS, Amiga, RISC OS, and others%0D%0A: DEC RT-11 and most other early non-Unix, non-IBM OSes, CP/M,MP/M, MS-DOS, OS/2, Microsoft Windows%0D: Commodore machines, Apple II family and Mac OS up to version 9Thanks pr and Bart. It seems the two options (''%0D'' and ''%0A'') need tobe corrected. How about this?var urlencode = (function() {var f = function(s) {return encodeURIComponent(s).replace(/%20/,''+'').replace(/(.{0,3})(%0A)/g,function(m, p1, p2) {return p1+(p1==''%0D''?'''':''%0D'')+p2;}).replace(/(%0D)(.{0,3})/g,function(m, p1, p2) {return p1+(p2==''%0A''?'''':''%0A'')+p2;});};try {if (f(''\n \r'') == ''%0D%0A+%0D%0A'') {return f;}}catch(e) {}})();Using try-catch is a nice way to avoid host object feature testing andensure that all the necessary parts of the regexp are working asexpected. This makes a full test of the new feature in a very smallspace.However, try-catch doesn''t appeal to some programmers. Any otheroption to test that replace can take a function for the secondargument?Peter 这篇关于url编码字符串(代码值推荐项目)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-09 20:44