JSP/Javascript event catcher

M

morc

hi,
i was wondering how to call a function upon clicking of a button... i
have abutton that uses some javascript to unhide a div... but at the
same time i would like it to call a method not part of javascript.

<form>
<input type="button" value="Generate Code"
onClick="display('code');"'<%UserBean.trackModels("n3",user,1);%>'"">
</form>


that is the code i tried... the javascript function works properly but
im unsure how to have the jsp method called.

i tried a system out println right after the calling trackModels and it
seems to execute when the page loads rather then when the button is
clicked. and the trackModels method doesn't get called at all.

any and all help is appreciated
thanks alot
-morc
 
O

Oliver Wong

morc said:
hi,
i was wondering how to call a function upon clicking of a button... i
have abutton that uses some javascript to unhide a div... but at the
same time i would like it to call a method not part of javascript.

<form>
<input type="button" value="Generate Code"
onClick="display('code');"'<%UserBean.trackModels("n3",user,1);%>'"">
</form>


that is the code i tried... the javascript function works properly but
im unsure how to have the jsp method called.

i tried a system out println right after the calling trackModels and it
seems to execute when the page loads rather then when the button is
clicked. and the trackModels method doesn't get called at all.

any and all help is appreciated
thanks alot

When the page is downloaded, the JSP has finished executing. No
connection is maintained between the server and the browser. After the JSP
has finished executing, and the page is downloaded, the browser will parse
the page, notice the JavaScript, and start running it. Again, the JSP has
finished executing, so it's too late to try to run further JSP code.

The only workaround is to have your javascript ask the browser to send a
new request to the server, where a new invocation of your JSP code will
occur.

- Oliver
 
M

morc

thanks that explains alot.

how do you suggest i should have the javascript reload the page but
also call the jsp method??
 
O

Oliver Wong

morc said:
thanks that explains alot.

how do you suggest i should have the javascript reload the page but
also call the jsp method??

I don't know much JavaScript. You might want to try asking this question
on a JavaScript related newsgroup. What you would want to do, at a very high
level, is to make a new request using JavaScript. Depending on the situation
and what is possible in JavaScript, you may wish to put information in the
GET or POST headers to explain to the JSP code exactly what it is you want
it to do. For example, perhaps pass in the name of the method you want to
have invoked.

- Oliver
 
M

morc

anyways thanks for kinda showing me hte light.

i found my self a solution by makin the button a submit and sending it
to another jsp page where it runs the method an redirects the user
back. thanks laot
 
H

Hal Rosser

morc said:
hi,
i was wondering how to call a function upon clicking of a button... i
have abutton that uses some javascript to unhide a div... but at the
same time i would like it to call a method not part of javascript.

<form>
<input type="button" value="Generate Code"
onClick="display('code');"'<%UserBean.trackModels("n3",user,1);%>'"">
</form>


that is the code i tried... the javascript function works properly but
im unsure how to have the jsp method called.

i tried a system out println right after the calling trackModels and it
seems to execute when the page loads rather then when the button is
clicked. and the trackModels method doesn't get called at all.
Oliver's is right. The javascript executes AFTER it gets to the user's
browser. The JSP code executed on the server.
JSP can see what the form element's values are at the time it gets the http
request (when the web page is called). You can only send another http
request to the jsp page.
Maybe you could have the javascript change a form element's value before you
submit the form so the JSP will respond to it in that way.
 

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

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,048
Latest member
verona

Latest Threads

Top