By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,654 Members | 1,580 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,654 IT Pros & Developers. It's quick & easy.

Passing valus through the url

P: n/a
Hi,
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 ?

takeExample.do?field='+field+'&Code='+code

this is the url im making

if code is 'trying + something else'
i get 'trying something else' from the request.getParameter

if it is 'trying & something else'
i get 'trying '
Please Help
Thnax in advance
Jul 23 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
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.
Jul 23 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.