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 l' Applet
Public void Init () {// methode Init ()
}
// Dessiner l' Applet
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>