• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Call a javascript function from a JSP

 
Celinio Fernandes
Ranch Hand
Posts: 549
Eclipse IDE Google Web Toolkit Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have one JavaScript function and I want to call
that function in Java Code (JSP).
Anyone knows a way to do it ?
Thanks
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think this <is possible. Java needs compiled code and Javascript is just a scripting language interpreted by the browser.
 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you be more specific? After the server has posted a response to the client you can only see html , javascriptcode in the source. If you want to
call the javascript from within a scriptlet then do something like this.

Other than this you can't call javascript from javacode. See above answer!
note! onKlick must be onclick
[ November 04, 2003: Message edited by: Mathias Nilsson ]
 
Mathias Nilsson
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://www.javaranch.com
 
B Mampilli
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Javascript can be executed from within a JSP scriptlet code only on the occurence of some event... for example onChange. So you can say...
input type=button value=callScript onChange=javascript:scriptFunction()
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Boney, I'm not sure what your point is. Your example has no JSP scriptlet code.
bear
 
Benny Tang
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi bear,
Boney means that javascript function can be called by using the event-driven in the output of the JSP. For example, you can invoke the javascript function when the user clicks the button...etc. We cannot invoke the javascript function in JSP scriplet directly. But we can make it invoke through the event happended in the output of JSP.
Benny T
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can program the JSP to execute some Javascript while it is loading on the client, and could have that Javascript call the server for some data.
These lines execute some action on the server.
The 'url' parameter is the URL to call

parseResult is a function that parses the returned data into the control (use innerHTML property).

This quite possibly will work only in IE 5.5 and up.
What you can NOT do is have the server execute a script on the client (the onload function happens only after the response has started sending to the client at which point you can't do much about it anymore.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65216
95
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
and could have that Javascript call the server for some data.

Umm, no it can't. Not in the traditional meaning of 'call'.
You can cause server-side data to be included in Javascript statements when builing them up in the text buffer to be sent to the browser. Or, you can cause Javascript, once sent to the browser and executing, to initiaite a new request. But you cannot call Java methods on the server from Javascript.
bear
[ November 05, 2003: Message edited by: Bear Bibeault ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Bear Bibeault:

Umm, no it can't. Not in the traditional meaning of 'call'.
You can cause server-side data to be included in Javascript statements when builing them up in the text buffer to be sent to the browser. Or, you can cause Javascript, once sent to the browser and executing, to initiaite a new request. But you cannot call Java methods on the server from Javascript.
bear
[ November 05, 2003: Message edited by: Bear Bibeault ]

That's what I meant to say...
The Javascript initiates a new request and parses the output of that request into a control.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic