Java - Javascript interaction

Articles

The mystery of the dead speaker

JSP File upload progress bar.

Self signed applets

Is it possible to control an applet with the use of Javascript? Is it possible for an applet to invoke Javascript methods? The answer is yes, to both questions. Great, so just how do we do it? read on to find out..

We will start off by going back the the HelloWorld applet that you learned in school. Most primers I read many years ago suggested overriding the paint() method. I still don't know why they took this approach so we will do things slightly differently.


import javax.swing.*;

public class JSHelloWorld extends JApplet {
    JTextArea txt = new JTextArea(100,100);
    public JSHelloWorld() {
        txt.setText("Hello World");
        getContentPane().add(txt);
    }
}

Sponsored Content

Rad SFTP - Web based graphical Secure FTP client

The set of HTML tags needed to embed this applet into a web page is well documented. We shall not delve into it. Instead we will look at what parameters we should use the applet and object tags to 'switch on' the java-js communications.

According to the java 5 documentation, if you use the APPLET tag, you do not need any special parameters to allow a javascript to call the applet's method. On the other hand if you use the OBJECT tag you need to use the scriptable parameter. Things use to be slightly different in the older versions. The rest of this page will examine how these tags should be used. If you want you can just skip ahead to the next page. All the code can be downloaded on the last page.

For an applet to call javascript methods, all you need to do is add the mayscript parameter. Thus our HTML will look like this:

<applet name="jsapplet"
		archive="jsapplet.jar"
		code="com.raditha.articles.JSHelloWorld"
		width="300" MAYSCRIPT
		height="300">
</applet>

As already mentioned, you need both the mayscript and scriptable parameters for the OBJECT tag. Your html will look like this:

<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
		width= "290" height= "290" style="border-width:0;"  id="rup" name="rup"
		codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4_1-windows-i586.cab#version=1,4,1">
		<param name="archive" value="jsapplet.jar">
		<param name="code" value="com.raditha.articles.JSHelloWorld">
		<param name="mayscript" value="yes">
		<param name="scriptable" value="true">
		<param name="name" value="jsapplet">
</object>

The object tag can actually cause the most recent version of the java plug in to be downloaded if the user does not have it installed or if the version present is older than the minimum requirement (in this case 1.4.1). If you want to be able to do the same for Netscape Navigator the object tag has to change slightly:

<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
	width= "290" height= "290" style="border-width:0;"  id="rup" name="rup"
	codebase="http://java.sun.com/products/plugin/autodl/jinstall-1_4_1-windows-i586.cab#version=1,4,1">
	<param name="archive" value="jsapplet.jar">
	<param name="code" value="com.raditha.articles.JSHelloWorld">
	<param name="mayscript" value="yes">
	<param name="scriptable" value="true">
	<param name="name" value="jsapplet">
</object>

All these differences between browsers makes me wish that I never took up programming. The above code samples have been combined together into a single PHP script that will detect which browser is used and display the appropriate tag (object or applet). You can download it here. You can also use the htmlconvertor, a part of the jdk to generate the full set of tags from the basic applet tag given above.

Introduction  |  Calling Applet Methods |  Calling javascripts
Copyright © Raditha Dissanayake 2013