Passing valus through the url

S

sancha

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
 
M

Michael Winter

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top