using JSObject, outside of main applet class

Discussion in 'Java' started by alex_the_hart@yahoo.com, Oct 28, 2005.

  1. Guest

    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
     
    , Oct 28, 2005
    #1
    1. Advertising

  2. wrote:

    > 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
     
    Andrew Thompson, Oct 31, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Pratul Chatterjee

    JSObject leaks on MSJVM

    Pratul Chatterjee, Jun 14, 2004, in forum: Java
    Replies:
    2
    Views:
    1,356
    Pratul Chatterjee
    Jun 15, 2004
  2. Minh Tran-Le
    Replies:
    0
    Views:
    460
    Minh Tran-Le
    Dec 2, 2004
  3. naveen
    Replies:
    5
    Views:
    5,607
    Abaddon
    Nov 13, 2008
  4. Richard Maher
    Replies:
    18
    Views:
    2,344
    Richard Maher
    Mar 27, 2009
  5. R
    Replies:
    0
    Views:
    360
Loading...

Share This Page