Question

Given an Applet in one frame in a multi-frame web application where all frames are loaded from the same web server, how can that Applet invoke JavaScript methods in other frames? I tried something like this:

jsobject.call("parent.otherFrame.methodToCall", new String[] {"argument"});

and I get a complaint that the function does not exist. However, if I invoke it like this:

jsobject.eval("parent.otherFrame.methodToCall('argument')");

then it works. I'm trying to avoid use of eval. Is calling code via call in a different frame from an Applet something that is likely to have different behavior in each browser and JVM combination? Is eval safer as it is evaluated in the JavaScript engine rather than partly on the Applet side?

Was it helpful?

Solution

The equivalent code without eval is probably something like this:

jsobject = (JSObject)jsobject.getMember("parent");
jsobject = (JSObject)jsobject.getMember("otherFrame");
jsobject.call("methodToCall", new String[] {"hello!"});

OTHER TIPS

My (limited) experience with a combination of Java applets, JavaScript and (the dreaded) frames, is that it is best to:

  • Include a script in the page/frame with the applet in it. Define a function intended to be called with 0 or more arguments (one of which might be the name of the target frame).
  • Call the function from the applet.
  • Let the JavaScript do the heavy lifting of finding the correct frame & doing whatever needs to be done.

This has a number of advantages.

  • It is easier to debug JS using the (for example) FF error console, than using the Java console.
  • JS can be updated more quickly and easily.
  • Each language encodes what it is good at doing. You can go into a Java editor to see the Java nicely formatted, and a JS editor to see the JS nicely formatted. As opposed to trying to read "JS written in Java" - a scarey experience.
  • When you ask for help on getting the script working, you have a neat example that the JS gurus can easily read!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top