The “JSObject” object is using for Applet to access the Javascript method.
JSObject win = JSObject.getWindow(this); win.call("callHello", new String[]{"mkyong"});
Tutorial
In this tutorial, you will use “JSObject” from Applet to execute a Javascript function.
1. Find the JSObject library
The “netscape.javascript.JSObject” is not in your classpath by default, you can find it inside your JRE lib folder, for example :
C:\Program Files\Java\jdk1.6.0_13\jre\lib\plugin.jar
The “plugin.jar” is what you want, get it and include it into your project classpath.
2. Create an Applet
Create an Applet and using JSObject to call a Javascript function, and pass a String variable “mkyong”.
package com.mkyong.applet; import java.applet.Applet; import java.awt.Button; import java.awt.FlowLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import netscape.javascript.JSObject; public class JavaToJavaScript extends Applet implements ActionListener{ Button button; private static final String EXECUTE = "EXECUTE"; public void init(){ setLayout(new FlowLayout()); button = new Button(); button.addActionListener(this); button.setActionCommand(EXECUTE); button.setLabel("Execute Javascript"); add(button); } @Override public void actionPerformed(ActionEvent e) { if(e.getActionCommand().equals(EXECUTE)){ JSObject win = JSObject.getWindow(this); win.call("callHello", new String[]{"mkyong"}); } } }
3. Create a HTML – MAYSCRIPT enabled
Due to some security concerns, The JSObject is not enabled by default, you have to include the “MAYSCRIPT” inside your Applet tag to enable it.
<html>
<script language="Javascript">
function callHello(name)
{
alert("hello " + name);
}
</script>
<head><title>Testing</title></head>
<body>
<h1>Applet acess Javascript method</h1>
<applet width=300 height=100 MAYSCRIPT
code="com.mkyong.applet.JavaToJavaScript.class">
</applet>
</body>
</html>4. Output
After Applet’s button clicked, it will use JSObject to call the Javascript function callHello() and pass in a string parameter – “mkyong”.
P.S Please read article about how to Javascript access Applet method.
Reference
1. How Java to Javascript Communication Works in Java Plug-in
2. Java to JavaScript Communication
http://www.mkyong.com/applet/how-to-javascript-access-applet-method/


