Wednesday 5 March 2008

java applet <> javascript

1. javascript call java applet: http://www.rgagnon.com/javadetails/java-0170.html

* make the variable you want to call public in the applet.
* in javascript just: document.myapplet.myfunction(some parametres);

2.1 java applet call variable or function defined in javascript:

* You can try an easy method, by calling a specially crafted URL using the showDocument() method, like this :
getAppletContext().showDocument(new URL("javascript:alert(Hello World);));
This doesn't work anymore using Internet Explorer as of a certain IE update from Microsoft dated some time ago this year, so it is easily broken.

* You can use the LiveConnect objects that are implemented in IE on Windows, Mozilla/Firefox on anything, Safari on Mac, etc.. it works almost everywhere.
For this you have to declare that your applet MAYSCRIPT, by adding a special parameter in the OBJECT/EMBED tag in your web page. (google for it if you need examples).
Then you have to add the "jaws.jar" (or "plugin.jar" if there's no jaws.jar) from the lib of the JRE to compile your Applet.
This jar should include the LiveConnext classes, and netscape.javascript.JSObject is the one you need.
Then You can then do something like this to achieve the same as solution one above :
JSObject browserWindow = JSObject.getWindow(this); //here 'this' is the JApplet
browserWindow.eval(buffer.toString());
Or you can navigate the DOM javascript tree of the WebPage using the methods of JSObject. (google for it if you need more info).

2.2 java applet call variable defined in html:
Just use the parametre of applet, for example:


1 comment:

Anonymous said...
This comment has been removed by a blog administrator.
My photo
London, United Kingdom
twitter.com/zhengxin

Facebook & Twitter