2

Every time I call a function of my applet from my Java Script, it throws an undefined exception. And my googling hasn't helped me at all.

here is a link to the site I am am hosting it on right now: Host Site

Here is my html for the embedding the Applet:

<object type="application/x-java-applet" 
            id="ClientApp" name="ClientApp" 
            archive="Cal.jar" 
            width="100" height="100">
        <param name="code"      value="Calendar_Algorithm" />
        <param name="mayscript" value="true" />
   </object>

And here is my java script code:

function test(){
        document.writeln("<p> "+"Test"+" </p>");
        try{
            var s=document.ClientApp.getGreeting();
            document.writeln("<p> First: "+s+" </p>");

        }catch(err){
            document.writeln("<p>Error Caught 1: "+err.description+"</p>");
        }


        try{
            var s=document.getElementById('ClientApp').getGreeting();
            document.writeln("<p> Second: "+s+" </p>");

        }catch(err){
            document.writeln("<p>Error Caught 2: "+err.description+"</p>");
        }


        document.close();
    }

I know it loads the applet because I can see the gui, and if it helps here is my init function

public void init() {

        try {
            SwingUtilities.invokeAndWait(new Runnable() {
                public void run() {
                    JLabel lbl = new JLabel(getGreeting());
                    add(lbl);
                }
            });
        } catch (Exception e) {
            System.err.println("createGUI didn't complete successfully");
        }
    }

here is a link to my full code as well Code

I got a feeling that the error is incredibly obvious, but I just can not see it.

Any help would be great!

P.S. The Applet class files are now in a signed jar file.

Also This will be placed in the webapps folder of a tomcat server, but I am currently accessing it as a local file.

8
  • err.description returns "undefined", getGreating(), returns a string "Hello" I have asked 8 questions, and only 1 of them has an answer that was a solution to the issue, though several of the others did lead to a solution, so I went back and accepted those. Commented Mar 20, 2011 at 22:10
  • I presume document.ClientApp is returning undefined? Have you tried accessing via ID? I'm so used to accessing applets by document.getElementById('appId').getGreeting(). Commented Mar 20, 2011 at 22:49
  • Does the exception occurs in the applet or in the JavaScript part? If the first, add a ex.printStackTrace() there, this gives more information. Commented Mar 20, 2011 at 22:54
  • Do you have some webspace with your applet + HTML-page, so we can look at it? Commented Mar 20, 2011 at 22:55
  • @jbrookover, I tried adding id, but no luck :( @Paulo Ebermann, it occurs in the JavaScript. The getGreeting() method executes in int init, and I know it works because the gui displays the correct message, and I have uploaded to my home server, see the question for the link Commented Mar 20, 2011 at 23:31

1 Answer 1

3

From http://www.w3.org/TR/html401/struct/objects.html#h-13.4, about the object attribute:

"This attribute names a resource containing a serialized representation of an applet's state." I predict this is not what you intended.

In addition, if you're on firefox mac you need the mayscript param for (JS-2-Java interaction) LiveConnect to work.

A way to deploy applets that works:

<object type="application/x-java-applet" width="100" height="100">
  <param name="codebase"  value="/applet_dir" />
  <param name="code"      value="Calendar_Algorithm" />
  <param name="mayscript" value="true" />
</applet>

If you don't have the Java console enabled you should definitely do so. It's enabled under the Java Control Panel advanced settings.

By the way, in Chrome Linux it works! In Firefox Linux it doesn't. Firefox doesn't like that both object and code param is specified and that the class names are different.

LiveConnect is buggy, especially on the mac. For an overview have a look at: applets-missing-information-about-liveconnect-and-deployment Basically you need to know what parts of LiveConnect to use and which not to.

Sign up to request clarification or add additional context in comments.

5 Comments

I have updated the question, and have incorporated your suggestions. I am still getting the undefined error, when I call the getGreeting() function
I didn't read you JavaScript code thoroughly enough the first time. You are using document.write which overwrites the content of the document, i.d., the object is removed. Try with console.log instead.
YOU ARE MY HERO!!!!! This has been bugging me for 3 days! I am new to java script and hadn't realized that once u start writing a document, you lost the current document!
Fantastic! Finally a way that works in IE6 (yes, we need that) and up and in Chrome and Firefox. Great!
Also 'scriptable' param may be required in some conditions.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.