Solved

Java & JavaScript Applet error

Posted on 2004-09-27
13
370 Views
Last Modified: 2013-12-16
I was trying to create an applet and I have some problems.

Here is the code:

// File: sleepclass.java
public class sleepclass
{
  public void sleep(float timeInMillis) throws InterruptedException
  {
    Thread.sleep((long)timeInMillis);
  }
}

// File: test.htm
<html>
<head></head>
<body>
<APPLET CODE="sleepclass.class" WIDTH=1 HEIGHT=1></applet>
<script language="javascript">
   window.status  = "abc";
   window.document.applets[0].sleep(2000);
   window.status += "def";
</script>
</body>
</html>


Then, I compile sleepclass.java:

c:\> javac sleepclass.java


When I launch test.htm, I get a JavaScript error in line 'window.document.applets[0].sleep(2000);'


Some idea?
0
Comment
Question by:Eligio
  • 9
  • 4
13 Comments
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
public class sleepclass extends java.applet.Applet

0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
you also probably want to fire the javascript when a button is pressed, etc...  As the applet will probably load into the browser AFTER the javascript has run...
0
 

Author Comment

by:Eligio
Comment Utility
Thanks TimYates, but the problem continues. Now I have:

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public void sleep(float timeInMillis) throws InterruptedException
  {
    Thread.sleep( (long) timeInMillis );
  }
}

// File: test.htm
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    window.document.applets[0].sleep(2000);
    window.status += "def";
  };
</script>
</head>
<body >
<APPLET CODE="sleepclass.class" WIDTH=100 HEIGHT=10></applet>
<input type="button" onclick="fncInit();">
</body>
</html>


But error is the same. I let the applet to finish loading and then I press the button, but I always get a JS error. The error description (traslated from spanish) is: Object doesn't accept this property or method.

Thanks
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    document.getElementById( 'VA' ).sleep( 2000 ) ;
    window.status += "def";
  };
</script>
</head>
<body >
  <applet id="VA" width="1" height="1" code="sleepclass" MAYSCRIPT>
  </applet>
  <input type="button" onclick="fncInit();">
</body>
</html>
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
If that doesn't work, try this for your applet (get rid of the "throws")

public class sleepclass extends java.applet.Applet
{
  public void sleep( int timeInMillis )
  {
    try { Thread.sleep( timeInMillis ); } catch( InterruptedException ex ) {}
  }
}

also, why were you passing "float" to this method?
0
 

Author Comment

by:Eligio
Comment Utility
TimYates, same error.

I used float beacause I read that javascript numeric arguments are always catched by the applet as floats, and then the call must be with a long ones.

Don't know what happends, beacause if I try to write a more simple applet, it always throws the same error.

public class A extends java.applet.Applet
{
  public void B( ) { //none }
}

When I do:
document.applets[0].B()
or
document.applets("my_applet_name").B()
or
document.my_applet_name.B()
or
document.getElementById(my_applet_id).B()

always get an error.

If I do: alert(document.applets[0].id), VA is printed, so I supose the applet is well referenced.


Any idea?


Thanks
0
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Try...

What browser are you using?

Tim

-----------------

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public String woo()
  {
    return "woo!" ;
  }
}

// File: test.htm
<html>
<head>
<script language="javascript">
  function fncInit () {
    window.status  = "abc";
    alert( window.getElementById( "VA" ).woo() ) ;
    window.status += "def";
  };
</script>
</head>
<body >
<APPLET id="VA" CODE="sleepclass" WIDTH=100 HEIGHT=10 MAYSCRIPT></applet>
<input type="button" onclick="fncInit();">
</body>
</html>
0
 

Author Comment

by:Eligio
Comment Utility
One more thing...

I have edited sleepclass.java, and add this code:

// File: sleepclass.java
public class sleepclass extends java.applet.Applet
{
  public void init()
  {
    System.out.println("We have been initialized");
  }
      
  public void sleep( int timeInMillis )
  {
    try { Thread.sleep( timeInMillis ); } catch( InterruptedException ex ) {}
  }
}

If I run: appletviewer test.htm, I can see "We have been initialized" on OS symbol.

But when I try to call the function ... crash!

Maybe this help you.


Thanks.
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
You do have the java pluing installed don't you?

Again, what browser are you using?  On what OS?

can you open the java console?  does that print "We have been initialised" when you open the webpage in your browser?
0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
And...what JDK are you using to compile the applet?
0
 
LVL 35

Accepted Solution

by:
TimYates earned 500 total points
Comment Utility
And what version of the plugin do you have installed in your browser?
0
 

Author Comment

by:Eligio
Comment Utility
Thanks TimYates.

After testing the the applet in other computer it worked. Then I reinstalled java and now it works fine too.



0
 
LVL 35

Expert Comment

by:TimYates
Comment Utility
Yay!! :-)

Good luck with it all! :-)

Tim
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Now that Expression Web 4.0 (http://www.microsoft.com/expression/products/Upgrade.aspx) is free if you buy or have the full version of Expression Web 3.0, now is the best time to  migrate from FrontPage to Expression Web (http://www.frontpage-to-exp…
Turn A Profile Picture Into A Cartoon Using Photoshop And Illustrator This tutorial will teach you how to make a cartoon style image out of a regular picture. I have tried to keep the tutorial as simple as possible. I used Adobe CS4 for this tuto…
The purpose of this video is to demonstrate how to add AdSense Ads to a WordPress Website, and how to set up WordPress to automatically place Ads in Sidebars. This will be demonstrated using a Windows 8 PC. Log into your AdSense account. : Cli…
The purpose of this video is to demonstrate how to set up an RSS Feed on a WordPress Website. This will be demonstrated using a Windows 8 PC. Feedburner will be used for this demonstration. Go to your WordPress login page. This will look like the…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now