EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

Call a Java method from Javascript


Author: Real Gagnon
Author's WebSite: http://tactika.com/realhome/realhome.html

You call a Java method by giving its fully qualified name. In the following snippet, the first example calls the method in the Toolkit to retrieve the screen resolution. The second example, calls a method in our Applet.

[Java applet]


 import java.awt.*;
 import java.applet.*;
 public class inJava extends Applet{
   public void sayHello() {
     Graphics g = getGraphics();
     g.drawString("Hello from JAVA!", 10, 10);
     }
   }



[Javascript and HTML]


<HTML><HEAD></HEAD><BODY>
<SCRIPT>
alert("Screen Dimension\n" +
" width:" +
java.awt.Toolkit.getDefaultToolkit().getScreenSize().width +
" height:" +
java.awt.Toolkit.getDefaultToolkit().getScreenSize().height);
</SCRIPT>
<FORM>
<INPUT type="button" value="call JAVA"
onClick = "document.myApplet.sayHello()">
</FORM>
<APPLET CODE="inJava.class"
NAME="myApplet"
HEIGHT=100 WIDTH=100>
</APPLET>
</BODY></HTML>


NOTE: On IE4, you can't call java.lang.* methods directly. IE javascript can only access the public methods of an applet (a class derived from java.applet.Applet) but don't have a general access to other java classes . So the solution is simple, wrap the java.lang.* call in a public method of a "dummy" Applet.

Posted On: 7-Jul-1999

internet.commerce



Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy