Jason B Herald x = sin x

30May/080

Applet and Javascript

So you have an applet and you want to do one or both of the following things:

  1. Run javascript commands from within your applet
  2. Control your applet through javascript

Step 1 Setup the Object Tag:

<object type="...">
<param name="mayscript" value="true">
<param name="scriptable" value="true">
</object>

*Note - I did not show the archive, code or other params needed to setup an object as they are dependent on your applet.
*Note - This setup allows for javascript communication in and out of your applet

Step 2 From within the java call some javascript

import javascript.*;
....
JSObject win = JSObject.getWindow(this);
win.eval("alert('test');");

*Note - To make this work you need to add plugin.jar from within your jdk/lib folder in the project classpath and include it in your deployment (archive = "..., plugin.jar").

And tada you can call java functions (yes the public ones) from javascript just by referencing the id of the object (dot) the function name. The translation between object types can get a bit tricky if you try to do a map or hash but with strings, ints and the like its pretty straight forward. The primary use for this was in setting up an event and using javascript to trigger it on submission of a form however the possibilites are endless ;-)

Filed under: Java, javascript No Comments
   

 

March 2010
M T W T F S S
« Feb    
1234567
891011121314
15161718192021
22232425262728
293031  

Categories

Pages