Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to call method of Java applet from JavaScript?

Status
Not open for further replies.

ChrisHenrich

Programmer
Sep 20, 2004
2
US
I am developing a web page in which I want to enable the reader to interact with a Java applet thru checkboxes outside the applet. I understand that JavaScript functions can call Applet methods, but it isn't working for me.

Here is a simple example: the html, then the JavaScript file, then the Java source.
---------------------example1.html
<HTML>
<HEAD>
<TITLE>Simple Swing Applet Example</TITLE>
<script type="text/javascript" src="TalkToApplet.js" >
</script>
</HEAD>
<BODY>
<APPLET archive="TalkToApplet.jar" code="TalkToApplet.class"
id="myapplet" width="300" height="150"
mayscript="mayscript">
Your browser does not support Java, so nothing is displayed.
</APPLET>
<input type="checkbox" onclick="sendWord('stuff'); return true;" >
</input>Send word to applet
</BODY>
</HTML>
-----------------------TalkToApplet.js
function sendWord(aWord)
{
var theAp = document.getElementById("myapplet");
var theAp1 = document.applets[0];
theAp.receiveWord(aWord);
return true;
}
------------------------TalkToApplet.java
import javax.swing.*;

public class TalkToApplet extends JApplet {

JTextArea showWord;

public void init() {
showWord = new JTextArea(1,10);
getContentPane().add(showWord);
showWord.setText("empty");
repaint();
}

public void receiveWord(String aWord){
showWord.setText(aWord);
repaint();
}

}
---------------------------------------------------
Working on Apple with Mac OS X, I have tested this with Safari, Internet Explorer, and Netscape 7.2. It fails on all three (hey, that's portability). On Netscape, I can examine the code with the JavaScript Debugger, which shows me that my variables "theAp" and "theAp1" both point to the intended applet tag in the HTML - but the attempt to call receiveWord() fails with an error message like this:
``TypeError: theAp.receiveWord is not a function''

Presumably I am leaving out some required thing, but what?

Thanks in advance to anybody who can enlighten me.
 
Thank you, chessbot!

It seems that <applet> is still supported, though deprecated. Your link set me thinking, and googling, and somewhere I found out that JavaScript - to - Java communication has not been supported under Mac OS X until the very latest release of the Java VM, 1.4.2. And the Mozilla family of browsers, including Netscape, do not use a VM later than 1.3.1, unless you install Java Embedding Plugin Enabler.

Now it works.
 
Great!

Applet will work, but your pages won't validate under the W3C Validator. I suggest you use <embed> or <object> for validation purposes.

--Chessbot

"DON'T PANIC
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top