Java Applets and JavaScript interaction

Enabling Java Applets and JavaScript interaction requires the netscape.javascript package as you can see in the source code below.

This package is included in a JAR file called JAWS.JAR which ships with Java Plug-in 1.3 and can also be found in the installation directory of Netscape Communicator (JAVA40.JAR).

Compile the source file with the classpath switch: javac -classpath PATH_TO_PACKAGE[;PATH_TO_OTHER_PACKAGE] Hello.java.

Examples




Source code of Hello.class

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

public class Hello extends Applet {
	 String string = "Applet Hello ready";
	 Font f = new Font("Verdana", Font.BOLD,12);

	 public String msg() {
		 return "Applet says Hello!";
	 }

	 public void msgAlert(String message) {
		 JSObject win = JSObject.getWindow(this);
		 try {
			win.eval("alert('Your message is: \\n" + message + "');");
		 } catch (Exception e) {
			string = message;
			repaint();
		 }
	 }
	 public void paint(Graphics g) {
		g.setFont(f);
		g.drawString(string,15,25);
	}
}