P
Peter Michaux
encodeURIComponent is commonly used to serialize forms for use with
XMLHttpRequest requests. For a perfect simulation of browser form
requests, 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</a> standardized format.
The handling of whitespace by encodeURIComponent() is different from
the x-www-form-urlencoded standard. According to the x-www-form-
urlencoded standard, a space should be encoded as a "+" and a newline
should 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 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?
Some developers may need to have the application/x-www-form-urlencoded
format. 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 sufficient
because we really need to know if the second argument to replace can
be a function. I imagine we can test this by using try-catch and
actually using replace with a function as the second argument. Does
anyone know a way to check without using try-catch?
For developers with server-side toolkits capable of dealing with the
output of encodeURIComponent the regexp work is not necessary and can
use 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 the
spaces 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 code
that 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/password
preference.
Peter
XMLHttpRequest requests. For a perfect simulation of browser form
requests, 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</a> standardized format.
The handling of whitespace by encodeURIComponent() is different from
the x-www-form-urlencoded standard. According to the x-www-form-
urlencoded standard, a space should be encoded as a "+" and a newline
should 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 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?
Some developers may need to have the application/x-www-form-urlencoded
format. 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 sufficient
because we really need to know if the second argument to replace can
be a function. I imagine we can test this by using try-catch and
actually using replace with a function as the second argument. Does
anyone know a way to check without using try-catch?
For developers with server-side toolkits capable of dealing with the
output of encodeURIComponent the regexp work is not necessary and can
use 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 the
spaces 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 code
that 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/password
preference.
Peter