How to invoke JavaScript Code from an Applet?
Hi friends,
I have done some basic coding in Java. Before that I have done C++, JavaScript and HTML programming language. Now recently I have started doing the Applets in Java. So I am not having much knowledge about an Applets. Now I want to invoke the Javascript from applet. So I thought to post you guys for help.!! Please tell me how to invoke JavaScript Code from an Applet? Expecting the reply sooner.:notworthy
Re: How to invoke JavaScript Code from an Applet?
Invoking the JavaScript code from an Applet is not tough task. Since, you are new that's why you don't know about it. Otherwise, I am sure that looking once you can understand to invoke the coding of JavaScript from Applet. But you should know some basic things first so that there would be no doubts while doing the coding. You should know that an Applets can invoke JavaScript functions present in the same web page as the applet. The class that enables applets to retrieve a reference to JavaScript objects and interact with the web page is the netscape.javascript.JSObject class.
Re: How to invoke JavaScript Code from an Applet?
A Javascript function is called from Java by using the showDocument method. A "javascript:" as the protocol is needed as a URL. The following code may help you in understanding the applets :
Code for Java applet :
Code:
import java.applet.*;
import java.net.*;
public class JavaEx extends Applet{
public void init(){
String msg = "Hello from Java (using javascript alert)";
try {
getAppletContext().showDocument
(new URL("javascript:doAlert(\"" + msg +"\")"));
}
catch (MalformedURLException me) { }
}
}
Code for Javascript and HTML :
HTML Code:
<HTML><HEAD></HEAD><BODY>
<SCRIPT>
function doAlert(s) {
alert(s);
}
</SCRIPT>
<APPLET CODE="JavaEx.class"
NAME="myApplet" MAYSCRIPT
HEIGHT=15 WIDTH=15>
</APPLET>
</BODY>
</HTML>
Re: How to invoke JavaScript Code from an Applet?
I am providing an example so that you can get the concepts more properly. In the following example of coding, you type in the TextField a Javascript function and press the button to execute the function. Here is the coding for that :
Code:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
import netscape.javascript.*;
public class JavaDemo extends Applet implements ActionListener {
Button b;
TextField tf;
public void init() {
tf = new TextField(25);
add(tf);
b = new Button("execute Javascript");
add(b);
b.addActionListener(this);
}
public void actionPerformed(ActionEvent ae) {
if (ae.getSource() == b) {
JSObject win = (JSObject) JSObject.getWindow(this);
win.eval(t.getText());
}
}
}
Re: How to invoke JavaScript Code from an Applet?
There is an another way to use the Reflection API. But this is very difficult to understand and is lengthy too. In this way you don't need to modify your CLASSPATH for compilation or even import the netscape.jsobject package. The following coding demonstrates the same :
Code:
import java.awt.event.*;
import netscape.javascript.*;
import java.lang.reflect.*;
...
// This goes in Applet Class
...
String jscmd = "window.close()";
String jsrst = null;
boolean success = false;
try {
Method getw = null, eval = null;
Object jswin = null;
Class c =
Class.forName("netscape.javascript.JSObject");
Method ms[] = c.getMethods();
for (int i = 0; i < ms.length; i++) {
if (ms[i].getName().compareTo("getWindow") == 0)
getw = ms[i];
else if (ms[i].getName().compareTo("eval") == 0)
eval = ms[i];
}
}
Object a[] = new Object[1];
a[0] = this;
jswin = getw.invoke(c, a);
a[0] = jscmd;
Object result = eval.invoke(jswin, a);
if (result instanceof String)
jsrst = (String) result;
else
jsrst = result.toString();
success = true;
}
catch (InvocationTargetException ite) {
jsresult = "" + ite.getTargetException();
}
catch (Exception e) {
jsresult = "" + e;
}
if (success)
System.out.println("eval succeeded, result is " + jsresult);
else
System.out.println("eval failed with error " + jsresult);
Re: How to invoke JavaScript Code from an Applet?
In order to call javascripts we need to get hold of a JSObject. But the most important thing that you need to do first, is to import the netscape.javascript.JSObject into our class. The given code explains the same :
Code:
public void init()
{
jso = JSObject.getWindow(this);
}
Now you need to add the JButton to our applet. Then you will have to add an ActionListener to the JButton, and it's actionPerformed method calls the javascript. The following code does the same :
Code:
public void actionPerformed(ActionEvent ae) {
if(jso != null )
try {
jso.call("updateWebPage", new String[] {txt.getText()});
}
catch (Exception exp) {
exp.printStackTrace();
}
}