Friday, February 15, 2008

Java - Javascript interaction

Many of us using javascript in daily coding.I am a java developer and using ajax with applet also.And i need to call applet methods from javascript.

Actually i knew this while i was facing a strange problem (ofcourse it was strange for me becasue i was not aware of this!). The applet used is compiled in JRE 1.4.0.And whenever anyone having JRE version newer than 1.4.0 tried accessing that applet, the call from javascript to applet method gives error "Object doesn't support this property". So i was very much tired of compiling applet with different versions of Jre but no luck.

So once i found the <param> tags for <Object> element which is now used for applet in HTML code.And then i used these tags for java and javascript interaction.

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.

For an applet to call javascript methods, all you need to do is add the mayscript parameter

so the tags are

<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="jssapplet">
</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>


Calling applet method from javascript is simple. You just need to do document.getElementById("appletid").methodName();
But calling javascript from Applet is somewhat tricky.

In order to call javascripts we need to get hold of a JSObject. But first things first, we need to import the netscape.javascript.JSObject into our class.

public void init()
{
jsObj= JSObject.getWindow(this);
}


All that remains to do is to add the JButton to our applet. Then we add an ActionListener to the JButton, and it's actionPerformed method calls the javascript.


public void actionPerformed(ActionEvent e) {
if(jsObj != null )
try {
jsObj.call("updatePage", new String[] {txt.getText()});
}
catch (Exception ex) {
ex.printStackTrace();
}
}


thats it! it will call the method from jsObj.

hope this helps someone like me for first time.Enjoy!

0 Comments: