Results 1 to 2 of 2

Thread: Passing JavaScript Variable to JSP Page

  1. #1
    Join Date
    Nov 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Passing JavaScript Variable to JSP Page

    Hi,

    I am new to JavaScript.

    I have read that there is a workaround to passing a JavaScript variable to a Java Server Page but would like confirmation that this is indeed possible and more information on exactly how to do this.

    I have a JavaScript function that is called with an event when the user selects an item from a drop-down. I successfully capture this item in my JavaScript function and assign it to a javascript variable. Now, I would like to pass that variable back to my JSP (which is in the same file as the javascript) and ultimately use that variable as a condition to a query.

    Anybody have ideas? Thanks.

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    You need some sort of link or some way to use the variable as a query, a simple link could be:

    HTML Code:
    <a id="passer" href="some.jsp">Whatever</a>
    Then in your javascript, once you have assigned the value to the variable, let's say the variable is named lookup and the value (doesn't really matter too much) this time is 7. Then you can do:

    Code:
    document.getElementById('passer').href='some.jsp?lookup='+escape(lookup);
    That is a very basic way. Depending upon the rest of your code and markup, the basic concept (creating a URL with the query string appended to it, and giving the user some way of navigating to the URL) may be applied in many different ways.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •