The following example shows how to call javascript Functions in the applet.
1. applet source code (tmin_JS.java)
// Importations
Import java. awt. Graphics;
Import java. awt. Event;
// LiveConnect... for JavaScript
Import netscape. javascript. JSObject;
Public class tmin_JS extends java. applet. Applet {
// Variables
// Initialisation de lapplet
Public void init () {// Methode init ()
}
// Dessiner lapplet
Public void paint (Graphics g) {// Methode paint ()
G. drawString ("Click here...", 5, 10 );
}
// Mouse down
Public boolean mouseDown (Event e, int x, int y ){
Try {// create JSObject
JSObject. getWindow (this). eval ("javascript: ale
Rt (tmin_JS click "+
"X =" + x + "y =" + y + ")");
}
Catch (Exception ex) {// Error on create JSObject
ShowStatus ("Error call javascript err =" + ex );
}
Return true;
}
}
Ii. Notes
(1) Add this line when calling the javascipt function:
JSObject. getWindow (this). eval ("javascript :...");
(2) Reference JSObject:
Import netscape. javascript. JSObject;
(3) Add the MAYSCRIPT mark when defining the applet.
<APPLET codeBase = "./" code = tmin_JS width = 80 height = 25 MAYSCRIPT>