Accessing a Javascript variable in JSP


Web tier: servlets, JSP, Web frameworks: Accessing a Javascript variable in JSP

  1. Accessing a Javascript variable in JSP (1 messages)

       Due to Avoidable reasons, i would like to Assign my javascript variable value to my jsp variable with in the same page. I tried to refresh the page once the value is assigned to my javascript variable. And then assigning the javascript variable to a hidden field by using the onload event of my form, but that is not the case.

    Thanks in Advance,
  2. Accessing a Javascript variable in JSP[ Go to top ]

    I am intrigued, as to what you are trying to do. What do you mean by JSP variable? It sounds like you might mean a bean in the page, request or session scope of your JSP. But this cannot be, as the Javascript only becomes active once it is in the browser, leaving the JSP context behind in the servlet engine, so you can no longer assign anything to it.

    So I assume you mean you want to assign a value to a HTML form field using javascript. To do this you need to assign the value to the "value" property of the field. Something like this:

     <body onload="document.forms['main'].elements['myField'].value = '6'">
      <form name="main">
       <input name="myField" type="hidden">