Calling javascript code from applet
Tuesday, November 27, 2007
Sometimes you may want your applet to interact with javascript.
Apply this code to your applet class.
private void alert(){
try{
getAppletContext().showDocument(
new URL("javascript:doAlert()"));
}catch (MalformedURLException ex) { }
}
When running this code you'll execute the javascript doAlert() placed in the htm file where the <applet> tag is located.
<script language="javascript" type="text/javascript">
function doAlert() {
//some code to be executed...
alert('doAlert() exevcuted!');
}
</script>
-OR-
Using the netscape.javascript package in your java code and the mayscript option in your applet tag. For this you need a Netscape JAR file name java40.jar to compile the javascript code.
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;
public class jsobject extends Applet implements ActionListener
{
Button button;
public void init()
{
button = new Button("hello");
button.addActionListener(this);
add(button);
}
public void actionPerformed(ActionEvent event)
{
JSObject obj = JSObject.getWindow(this);
obj.call("hello", null);
}
}
Regards,
Manesh Koovappillil
Labels: applet, code, java, javascript, Netscape JAR
posted by MIGHTYMAK @ 2:05 AM,