Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Call a javascript function from a JSP

 
Ranch Hand
Posts: 563
Google Web Toolkit Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.javaranch.com
 
Ranch Hand
Posts: 61
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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()
 
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Boney, I'm not sure what your point is. Your example has no JSP scriptlet code.
bear
 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67595
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic