Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to call Javascript function in Java

Posted on 2012-04-10
4
683 Views
Last Modified: 2012-08-13
Developer wrote a code in Java .I want to write new functionality add button but thing is Previous developer wrote a code in Java Class..
Now I have  add a  button and call a function
function in java servlet
public chkpop()
{
..
//code I write ..
            out.write("<tr> <td><input type=button name=Excel value=excel onclick='javascript:test()' > </td></tr>");

}

not sure how to call Java function from onclick or should I Call Javascript function from Java class but then using Ajax I can all java function ...

but If i used above code where  calling  test() a java script function but Where I have to define the Javascript function  test(){} ?

test function should be define in JSP or Javascript file ..How I define in Java class where test() function lies in JSP or Javascript file ..

Or is there any way I can Call Java function on Onclick="<%test()%>"
0
Comment
Question by:harshalpotdar
4 Comments
 
LVL 20

Expert Comment

by:chaitu chaitu
ID: 37831108
test function should be define in JSP or Javascript file

you can define either in jsp(withing script blocks) file or in js file.
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 37831413
and onclick='javascript:test()'  should just be
onclick='test()'

unless you have VBScript as the first script block in the browser

onclick is not the event handler I would choose on an input FIELD - a button yes (except the submit button - use onsubmit of the form instead)
0
 
LVL 28

Expert Comment

by:Pravin Asar
ID: 37832108
0
 
LVL 11

Accepted Solution

by:
SANDY_SK earned 500 total points
ID: 37851975
hi harshalpotdar,

You cannot just call a server side java code on click of a button on the client side. But there is one way of doing it, that is using DWR (http://directwebremoting.org/dwr/index.html), Even in this case what he does internally is that he uses AJAX and gives you the convenience of calling server side java code directly from javascript.

However if you do not want to use that, what you could do is write the AJAX code in a javascript file(YourScriptFile.js) and then include it like this

public chkpop()
{
..

//Line added
out.write("<script language=\"javascript\" src=\""+request.getContextPath()+"\"/js/YourScriptFile.js\"></script>");

//code I write ..
out.write("<tr> <td><input type=button name=Excel value=excel onclick='javascript:test()' > </td></tr>");

}

Open in new window


OR

Another way is that (Though this is not a great way of doing things) you could write the fill javascript code using out.println, something like this


public chkpop()
{
..
out.println("<script>");
out.println("    function test() { ");
out.println("        alert('This is a test function'); ");
out.println("        //Your rest of the AJAX logic can come here; ");
out.println("    } ");
out.println("</script>");

//code I write ..
out.write("<tr> <td><input type=button name=Excel value=excel onclick='javascript:test()' > </td></tr>");

}

Open in new window

0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java imports not found 4 16
jQuery on Submit 4 36
HTML Anchor Link Problem 3 35
Search Item in Table 2 18
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
The viewer will learn how to count occurrences of each item in an array.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

792 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question