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 Javascript from a Java applet


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

Netscape only

A Javascript function is called from Java by using the showDocument method. A URL is needed with "javascript:" as the protocol.

[Java applet]


 import java.applet.*;
 import java.net.*;

 public class inJava extends Applet{
   public void init(){
     String msg = "We will jump to an HTML tag";
     try {
       getAppletContext().showDocument
         (new URL("javascript:doAlert(\"" + msg +"\")"));
       getAppletContext().showDocument
         (new URL("javascript:jumpTo(\"#JUMP\")"));
       }
     catch (MalformedURLException me) { }
     } 
   }



[Javascript and HTML]


<HTML><HEAD></HEAD><BODY>
<SCRIPT>
function doAlert(s) {
alert(s);
}
function jumpTo(tag) {
self.location=tag;
}
</SCRIPT>
<APPLET CODE="inJava.class"
NAME="myApplet" MAYSCRIPT
HEIGHT=400 WIDTH=10>
</APPLET>

jump here from Java via Javascript
</BODY>
</HTML>


Netscape AND Microsoft Internet Explorer v4

With IE4, Microsoft included the Netscape package netscape.javascript.* to allow a JAVA applet access to Javascript objects. To compile such Applet, you must adjust the CLASSPATH to include the Netscape java40.jar or Microsoft classes.zip. To use the Netscape JAR for example, the CLASSPATH will look like this:

set CLASSPATH=
.;c:\windev\jdk1.1.3\lib\classes.zip;
c:\Program Files\Netscape\Communicator\Program\Java\Classes\java40.jar;

IN the following example, you type in the TextField a Javascript function and press the button to execute the function. For example, try alert('Hello from JAVA'). Or you can execute function defined on the same page as the Applet. The Applet must contains the MAYSCRIPT parameter to be able to use JSObject.


 import java.applet.*;
 import java.awt.*;
 import java.awt.event.*;
 import netscape.javascript.*;

 public class jsJAVA extends Applet implements
     ActionListener {
   Button b;
   TextField t;
   
   public void init() {
      t = new TextField(20);
      add(t);
      b = new Button("execute Javascript");
      add(b);
      b.addActionListener(this);
      }
   
   public void actionPerformed(ActionEvent ae) {
      if (ae.getSource() == b) {
        JSObject win = (JSObject) JSObject.getWindow(this);
        win.eval(t.getText());
        }
      }
   }



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