views:

78

answers:

1

I have some long-running Java code running in a thread started by a Java applet. As soon as the code has finished, it has information for the user. I'd like to pass this information to a JavaScript callback in a thread-safe way. Just using the Java DOM API to modify the HTML document is not good enough, unless my JavaScript callback gets called as a side effect in a thread safe way (i.e. eventually, in the same browser thread where JavaScript callbacks execute). How can I implement this? Are there libraries already doing it?

A: 

Looks as though the Common DOM API is what you need. In essence you request a DOMService to call you back on the main UI thread when it is good and ready.

http://download-llnw.oracle.com/javase/6/docs/technotes/guides/plugin/developer_guide/java_js.html#common_dom

As I understand things, JSObject is the "old way" and the Common DOM API is the newer way (as of Java 6). What you need to do is call a method of your applet and pass the function object that you want to have called back. Your applet then calls that function object from within your DOMAction.

I have no code at hand to show you and this is not something I've done before using Java. However I have used a similar technique from with an NPAPI web plugin written in C++ i.e. had JS call my C++ object passing a function as a parameter, and then at a later stage, have the C++ object call the JS function. 'hope that this helps.

Christopher Hunt
Sure, but what do I write to my in my `DOMAccessor.run`? Should I try to get a reference to a `JSObject`, and try to invoke a callback from there? Or should I try to modify the DOM, and hope that JavaScript can detect that (e.g DOMAttrModified) -- but this is supported only in the minority of the browsers, and not supported in Internet Explorer. Could you please give a cross-browser code example that works?
pts