On 4 Jan 2005 20:26:25 -0800, sancha <ch**************@gmail.com> wrote:
i am trying to pass values to a struts action using javascript. It
works fine until the value has something like '+' or '&' sign in it. Is
this a problem with javascript or these values cannot be passed in the
URL ?
They must be escaped. The ampersand (&) is used to separate name/value
pairs, and plus (+) can be used (but it's not the only way) to replace
spaces. Call encodeURIComponent with the values you're going to add.
takeExample.do?field='+field+'&Code='+code
'takeExample.do?field='
+ encodeURIComponent(field) + '&Code='
+ encodeURIComponent(code)
Unfortunately, encodeURIComponent isn't implemented by older browsers.
However, you can emulate it:
if('function' != typeof encodeURIComponent) {
this.encodeURIComponent = function(uri) {
var x = /[\w.!~*'()-]/, r = '';
for(var c, i = 0, n = uri.length; i < n; ++i) {
if(x.test(c = uri.charAt(i))) {r += c;}
else {
c = uri.charCodeAt(i).toString(16).toUpperCase();
r += ((c.length == 1) ? '%0' : '%') + c;
}
}
return r;
};
}
The code above will determine whether the function is implemented by the
browser. If not, a basic implementation[1] will be created.
Mike
[1] By "basic implementation" I'm referring to the fact that
encodeURIComponent should be able to deal with Unicode characters. The
implementation above only concerns itself with the ISO-8859-1 (Latin)
character set.
--
Michael Winter
Replace ".invalid" with ".uk" to reply by e-mail.