my dog learned polymorphism*
The moose likes JSP and the fly likes Call a javascript function from a JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Call a javascript function from a JSP" Watch "Call a javascript function from a JSP" New topic
Author

Call a javascript function from a JSP

Celinio Fernandes
Ranch Hand

Joined: Jun 28, 2003
Posts: 547

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


SCJP 1.4, SCWCD 1.4, SCBCD 1.3, SCBCD 5
Visit my blog
Anonymous
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Oct 13, 2003
Posts: 107
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 ]

SCJP2 , MCP( 70-229 ) , Preparing For SCWDC
Mathias Nilsson
Ranch Hand

Joined: Oct 13, 2003
Posts: 107
http://www.javaranch.com
B Mampilli
Ranch Hand

Joined: Aug 05, 2002
Posts: 61
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()


Boney
SCJP, SCWCD
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

Boney, I'm not sure what your point is. Your example has no JSP scriptlet code.
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Benny Tang
Greenhorn

Joined: Aug 03, 2003
Posts: 26
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

Joined: Oct 12, 2000
Posts: 5093
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.


42
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61661
    
  67

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

Joined: Oct 12, 2000
Posts: 5093
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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Call a javascript function from a JSP