1

I am new in JavaScript. I want to know how to call a simple method in Java file from JavaScript program. Could you please give me a simple example as well?

Thank you

3
  • 1
    Please find your shift key & use it (once at the start of every sentence, and for every use of the word 'I' at the very least). This is a technical forum, not a social networking site. Commented Mar 23, 2011 at 4:09
  • By Java file you mean an Applet or Application or server side code (Servlet)? Commented Mar 23, 2011 at 4:11
  • I think you need to explain what do you mean by "Java file". Is it a Java file lying on your machine, an Applet or a server side method? Commented Mar 23, 2011 at 4:22

2 Answers 2

5

Javascript will not be able to access server side java code directly. It will need to make an ajax call to access a method or make a http call.

If you have applets you will be able to access the applet java code.

See example here http://download.oracle.com/javase/1.5.0/docs/guide/plugin/developer_guide/js_java.html

Sign up to request clarification or add additional context in comments.

1 Comment

thank you. i thought i can call java method directly. i'll try to use applet.
3

When you say "java file" I presume you mean a Java applet. In that case, say you have this applet code on your page:

<applet id="myapplet" class="MyApplet.class" ... />

If you wanted to call the helloWorld() method in that applet, you simply need to do this:

<script type="text/javascript>
    document.getElementById['myapplet'].helloWorld();
</script>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.