JSP and Javascripts on same page (Passing values to JSP fromJavascript)

Discussion in 'Javascript' started by Husain, Apr 14, 2008.

  1. Husain

    Husain Guest

    Hello.

    I have a button on a form which goes to a function validate in
    javascript as shown below:

    <input type="button" name="<%= i%>" value="Edit"
    onclick="validate(this.name)">

    The javascript:

    <script language="javascript">
    var Action="";
    function validate(Action){
    var actstring = Action.toString();
    ivalue = parseInt(actstring);
    return confirm('Ok for this action : '+ivalue+' ?'); //this is to
    test
    }
    </script>

    I am changing the ivalue to an integer which eventually will access an
    array built on the same page but using JSP. How can I pass the ivalue
    back to the JSP?

    Thank you.
     
    Husain, Apr 14, 2008
    #1
    1. Advertising

  2. Husain

    Tom de Neef Guest

    Re: JSP and Javascripts on same page (Passing values to JSP from Javascript)

    "Husain" <>

    > I am changing the ivalue to an integer which eventually will access an
    > array built on the same page but using JSP. How can I pass the ivalue
    > back to the JSP?
    >


    Not very elegant: store it (as a string) in an invisible element of the
    document. JSP can read it there.
    Tom
     
    Tom de Neef, Apr 14, 2008
    #2
    1. Advertising

  3. Husain wrote:
    > I have a button on a form which goes to a function validate in
    > javascript as shown below:
    >
    > <input type="button" name="<%= i%>" value="Edit"
    > onclick="validate(this.name)">
    >
    > The javascript:
    >
    > <script language="javascript">


    <script type="text/javascript">

    > var Action="";
    > function validate(Action){


    By good convention, only identifiers referring constructors and constants
    should begin with a capital letter. You should know that one from Java.

    > var actstring = Action.toString();


    The type conversion is unnecessary as the value is a string value already.

    > ivalue = parseInt(actstring);


    Should be:

    ivalue = parseInt(actstring, 10);

    But since you are setting the value and not the user, this conversion is
    unnecessary as well, at least in this particular code snippet.

    > return confirm('Ok for this action : '+ivalue+' ?'); //this is to
    > test
    > }
    > </script>
    >
    > I am changing the ivalue to an integer which eventually will access an
    > array built on the same page but using JSP. How can I pass the ivalue
    > back to the JSP?


    Suppose you have

    <form action="...">
    ...
    <input type="hidden" name="formaction" value="">
    ...
    </form>

    you could write something along

    <form action="..." onsubmit="validate(this)">
    <script type="text/javascript">
    function validate(f)
    {
    var action = arguments.callee.action;
    f.action = action;
    return window.confirm("Ok for this action: ' + action);
    }
    </script>

    <input type="submit" name="foo" value="Foo me!"
    onclick="validate.action = this.name;">
    </form>

    But, as I said before in <news:>, you should use

    <form action="handles_foo_and_bar_submits.jsp" ...>
    ...
    <input type="submit" name="foo" value="Foo me!">
    <input type="submit" name="bar" value="Bar me!">
    </form>

    instead, so that your application degrades gracefully. Since this is not
    exactly a new problem, I suggest you read these articles I posted not long ago:

    <news:>
    <news:>


    A request in advance: *Please trim your quotes to the necessary minimum.*


    PointedEars
    --
    Use any version of Microsoft Frontpage to create your site.
    (This won't prevent people from viewing your source, but no one
    will want to steal it.)
    -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
     
    Thomas 'PointedEars' Lahn, Apr 14, 2008
    #3
  4. Tom de Neef wrote:
    > "Husain" <>
    >> I am changing the ivalue to an integer which eventually will access an
    >> array built on the same page but using JSP. How can I pass the ivalue
    >> back to the JSP?

    >
    > Not very elegant: store it (as a string) in an invisible element of the
    > document. JSP can read it there.


    Not just any element, but <input type="hidden" ...> in the form to be
    submitted. Else JSP will _not_ get at the value.


    PointedEars
    --
    var bugRiddenCrashPronePieceOfJunk = (
    navigator.userAgent.indexOf('MSIE 5') != -1
    && navigator.userAgent.indexOf('Mac') != -1
    ) // Plone, register_function.js:16
     
    Thomas 'PointedEars' Lahn, Apr 14, 2008
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ron Spears
    Replies:
    4
    Views:
    408
    Grant Wagner
    Aug 12, 2003
  2. Elf M. Sternberg
    Replies:
    1
    Views:
    434
    Andrew Clover
    Jul 5, 2003
  3. Replies:
    4
    Views:
    22,346
    Mirunalini
    Jun 24, 2011
  4. Ric Pullen

    Can someone ExplainRun at server and Client javascripts

    Ric Pullen, Jul 11, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    110
    Ric Pullen
    Jul 11, 2003
  5. Grzegorz ¦lusarek

    AJAX and disapeard javascripts tags <script></script>

    Grzegorz ¦lusarek, Oct 11, 2005, in forum: Javascript
    Replies:
    1
    Views:
    119
    knocte
    Oct 12, 2005
Loading...

Share This Page