Passing valus through the url

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • sancha

    #1

    Passing valus through the url

    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.getPara meter

    if it is 'trying & something else'
    i get 'trying '


    Please Help
    Thnax in advance
  • Michael Winter

    #2
    Re: Passing valus through the url

    On 4 Jan 2005 20:26:25 -0800, sancha <chaudhuri.sand ip@gmail.com> wrote:
    [color=blue]
    > 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 ?[/color]

    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 encodeURICompon ent with the values you're going to add.
    [color=blue]
    > takeExample.do? field='+field+' &Code='+code[/color]

    'takeExample.do ?field='
    + encodeURICompon ent(field) + '&Code='
    + encodeURICompon ent(code)

    Unfortunately, encodeURICompon ent isn't implemented by older browsers.
    However, you can emulate it:

    if('function' != typeof encodeURICompon ent) {
    this.encodeURIC omponent = 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
    encodeURICompon ent 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.

    Comment

    Working...