using JSObject, outside of main applet class

A

alex_the_hart

I want to use JSObject to trigger some javascript events, but I have a
fairly complicated piece of code, with many classes, and I want to call
JSObject from outside the main applet class.

The problem is this: The method that calls JSObject cannot be static,
because it needs to access the applet. So I need to call the method
with an object, but the only object I can use is the top level applet,
which isn't instantiated in my code, but by the browser.

This is some test code, to give an idea of what the problem is:

public class testJs extends JApplet implements Runnable
{
// this is the main applet

public void runJs(String str) // this function cannot be static,
//because it contains "this"
{
JSObject win = (JSObject) JSObject.getWindow(this);
win.eval("alert(1);");
}

}

public class callJs
{
// this class is where the js needs to be called.
testJs.runJs(); // this doesn't work because it needs an object
// to run it with, but the only object I can use
// is the top level applet. But how can I reference
// the top level object?
}

Does anyone have a suggestion as to how this can be accomplished?

Many thanks for you help.

- Alex
 
A

Andrew Thompson

public class callJs

// please use the common naming conventions for
// classes and methods. Classes are
// EachWordUpperCase

private testJs applet;

public CallJS(testJs theApplet) {
applet = theApplet;
}

public void someMethod() {
// this class is where the js needs to be called.
testJs.runJs(); // this doesn't work because it needs an object
applet.runJs();

// to run it with, but the only object I can use
// is the top level applet. But how can I reference
// the top level object?
}

Does anyone have a suggestion as to how this can be accomplished?

Many thanks for you help.

Please consider asking such questions in groups better
suited to beginners.
<http://www.physci.org/codes/javafaq.jsp#cljh>

HTH
 

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,769
Messages
2,569,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top