jGuru
Register Email     Password Forgot your
password?
HOME FAQS FORUMS DOWNLOADS ARTICLES PEERSCOPE LEARN

  Search   jGuru Search Help

Question How do I communicate with an applet from JavaScript?
Derived from A question posed by John Zukowski PREMIUM
Topics Java:API:Applets, JavaScript:Integration:Java
Author Sameer Tyagi
Created Feb 22, 2000 Modified Jul 26, 2001


Answer
You can use use JavaScript to diretly call public methods in the applet or access public variables. JavaScript treats the embedded applet as an object. In the applet tag give the applet a name (for Netscape to recognize the applet), and an Id (for IE to recognize the applet).

Consider the example below where the applet has 2 methods public void increment() which increments a counter and public void decrement() which decrements the counter.

The HTML page would look something like :

<HEAD>
<SCRIPT LANGUAGE="JavaScript">

</SCRIPT>

</HEAD>


<APPLET CODE="MyApplet.class" 
   width=200 height=200 
   name=counter ID=counter>
</APPLET>


<FORM NAME>
<INPUT TYPE="BUTTON" VALUE="Increment"  
  OnClick="incrementIt();">
<INPUT TYPE="BUTTON" VALUE="Decrement" 
  OnClick="decrementIt();">
</FORM>
Note: There are issues regarding JavaScript - Java communications on the Mac. They don't work.

For additional information see http://www.codeproject.com/jscript/javatojs.asp.

Is this item helpful?  yes  no     Previous votes   Yes: 4  No: 0



Comments and alternative answers

Comment on this FAQ entry

Code Missing
Marcus Lamb, Feb 6, 2004
There is a JavaScript function missing from this code example. It should read:

<HEAD>
<SCRIPT LANGUAGE="JavaScript">

function incrementIt(){
document.counter.increment()
//OR ...
//document.applets[0].increment()
}

function decrementIt(){
document.counter.decrement()
//OR ...
//document.applets[0].decrement()
}

</SCRIPT>
</HEAD>
<APPLET CODE="MyApplet.class"
width=200 height=200
name=counter ID=counter>
</APPLET>
<FORM NAME>
<INPUT TYPE="BUTTON" VALUE="Increment"
OnClick="incrementIt();">
<INPUT TYPE="BUTTON" VALUE="Decrement"
OnClick="decrementIt();">
</FORM>


Is this item helpful?  yes  no     Previous votes   Yes: 2  No: 0



Reply to this answer/comment  Help  


Ask A Question



 
Related Links

Applets FAQ

Applets Forum

Java 1.2 Demonstration Applets

Java Tutorial Applets Trail

JavaScript FAQ

JavaScript Forum

Netscape JavaScript Reference

Focus on JavaScript

Wish List
Features
About jGuru
Contact Us

 



The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers