Java Applet<-->JavaScript communication (Firefox 3).

D

Daniel Pitts

I only need to support Firefox 3 at the moment, so any advice can be
specific to that.

I have a Java Applet, lets call it HelperApplet.

It has one method of importance:
public Object doHelp(JSObject callback) {
System.out.println(callback + "\n" + callback.getMember("foo"));
callback.call("helped", new Object[] {callback.getMember("foo")});
return callback;
}

I have javascript that does this:

myCallback = documents.applets[0].doHelp({
foo: "bar",
helped: function(result) {
alert(result);
}
});

Unfortunately, the callback doesn't get called. It appears the the
JSObject callback doesn't contain any members in Java land. Although, I
can access the returned object, eg myCallback.helped("Broken Java") works.

Any advice? Work-arounds? Are my attempts probably going to be futile?

Thanks,
Daniel.
 
D

Daniel Pitts

Daniel said:
I only need to support Firefox 3 at the moment, so any advice can be
specific to that.

I have a Java Applet, lets call it HelperApplet.

It has one method of importance:
public Object doHelp(JSObject callback) {
System.out.println(callback + "\n" + callback.getMember("foo"));
callback.call("helped", new Object[] {callback.getMember("foo")});
return callback;
}

I have javascript that does this:

myCallback = documents.applets[0].doHelp({
foo: "bar",
helped: function(result) {
alert(result);
}
});

Unfortunately, the callback doesn't get called. It appears the the
JSObject callback doesn't contain any members in Java land. Although, I
can access the returned object, eg myCallback.helped("Broken Java") works.

Any advice? Work-arounds? Are my attempts probably going to be futile?

Thanks,
Daniel.
I figured out a work around, not pretty, but it does the trick:

public class MyApplet extends JApplet {
private JSObject appletTmp;

public JSObject resolveObject(Object o) {
final int hashCode = System.identityHashCode(o);
appletTmp.setMember("toResolve" + hashCode, o);
return (JSObject) appletTmp.getMember("toResolve" + hashCode);
}

public void init() {
if (appletTmp == null) {
final JSObject window = JSObject.getWindow(this);
final String tmpName = "_AppletTmp" +
System.identityHashCode(this);
window.eval("var "+ tmpName +" = {}");
appletTmp = (JSObject)window.getMember(tmpName);
}
}

public Object doHelp(Object o) {
JSObject callback = resolveObject(o);
System.out.println(callback + "\n" + callback.getMember("foo"));
callback.call("helped", new Object[] {callback.getMember("foo")});
return callback;
}
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top