JavaScript callback from Applet.

D

Daniel Pitts

Is there a way to pass actual JSObject from JavaScript into an Applet?

I'm attempting something like:
documents.applets[0].myMethod({
foo: 'bar',
baz: function(arg) {
alert(arg);
}
);
I thought it would work. applets[0] has mayscript enabled, etc...

public class MyApplet extends JApplet {
public void myMethod(JSObject obj) {
System.out.println(obj.getMember("foo")}; // prints null.
obj.call("baz", new Object[] {"Joe Bob"}); //does nada.
}
}

Is what I'm trying to do possible? I'd rather not pass in Strings if
possible, I'd like to pass in actual JSObjects.

Thanks,
Daniel.
 
D

Daniel Pitts

Daniel said:
Is there a way to pass actual JSObject from JavaScript into an Applet?

I'm attempting something like:
documents.applets[0].myMethod({
foo: 'bar',
baz: function(arg) {
alert(arg);
}
);
I thought it would work. applets[0] has mayscript enabled, etc...

public class MyApplet extends JApplet {
public void myMethod(JSObject obj) {
System.out.println(obj.getMember("foo")}; // prints null.
obj.call("baz", new Object[] {"Joe Bob"}); //does nada.
}
}

Is what I'm trying to do possible? I'd rather not pass in Strings if
possible, I'd like to pass in actual JSObjects.

Thanks,
Daniel.
I figured out a work around, not pretty, but it does the trick.
Basically, the JSObject passed to the applet has the right native ID,
but not the correct context. I can fix this by putting it into a
JSObject that does have the correct context, and pulling it back out:

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 doCallback(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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,071
Latest member
MetabolicSolutionsKeto

Latest Threads

Top