Optimizing the loading process for Java applets

Source: Internet
Author: User
Tags visibility

As you know, when Java applets are not fully loaded, ie or Netscape displays a gray area in the corresponding area of the applet. So, have you ever thought of replacing it in other ways?

This article describes how to use JavaScript implementations to display a message when loading Java applets, such as waiting for a while. This piece of code can be run in IE and Netscape.

The following code can solve this problem.

<script language= "JavaScript"
<!--Hide script from old browsers
function init () {
//M    Icrosoft Internet Explorer
if (document.all) {
document.all.loading.style.visibility= "hidden";
document.all.myapplet.style.visibility= "visible";
Document.applets[0].repaint ();   
}
//Netscape Navigator
else{
document.loading.visibility= "Hide";
document.myapplet.visibility= "visible";
}

//;
</script>
<body onload= "init ()"
<div id= loading "style=" posit Ion:absolute;left:150;top:10; "
Please wait while Java applet loads ...
</div>
<div id= "MyApplet" style= "Position:absolute;left:150;top:10;visibility:hidden"
<applet code= "Charts.charts.class" archive= "Charts.jar" width= "" height= "codebase=" "VIEWASTEXT>
<param name= "Xvalue" value= 1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
23,24,25,26,27,28,29,30 "
<param name=" Yvalue "value=" 22,21,19,23,18,24,28,30,27,22,20,18,16,18,19,16,17,14,12,
13,16,18,19,20,16,17,13,10,14,19,25 "
</applet>
</div>
</body>

Explain:

First, let's take a look at what we really want to do. In fact, we just want to download the Java program in the process of the unsightly gray block removed, to some messages such as loading, please wait for the screen to cover the original gray area. Once the Java load is complete, restore the area to the applet as soon as it is visible.

The JavaScript code above is an example of how this is accomplished.

Now, let me explain how we did it.

First, look at the following sections.

<div id="loading" style="position:absolute;left:150;top:10;">
Please wait while Java applet loads...
</div>
<div id="myapplet" style="position:absolute;left:150;top:10;visibility:hidden">
<applet code="charts.charts.class" archive="charts.jar" width="600" height="400" codebase="" VIEWASTEXT>
<Param name="xValue" value="1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,
23,24,25,26,27,28,29,30">
<Param name="yValue" value="22,21,19,23,18,24,28,30,27,22,20,18,16,18,19,16,17,14,12,
13,16,18,19,20,16,17,13,10,14,19,25">
</applet>
</div>

As you can see, I've used 2 layers, the first layer (ID loading) to write some hint messages. The 2nd layer (ID myapplet) is not visible at the beginning. When the applet is downloaded, display the 2nd layer immediately and make the first layer invisible. So, as long as these 2 layers are the same size, the position must be exactly the same. We can achieve our goals.

Now we only have one last question to solve: how do you know that the applet has been completely downloaded? This property is provided in IE and Netscape, and is true when the entire contents of the page (including applets, pictures, sounds, etc.) have been downloaded document.all. OK, so we just need to monitor if document.all is true, and if so, you can set the layer of the message to be invisible, the layer where the applet is located is visible, and the applet's display is refreshed (because the display of the previous applet is hidden), otherwise the reverse is done.

This is the code after the process:

<script language="JavaScript">
  function init()
  {
   if (document.all)
   {
    document.all.loading.style.visibility="hidden";
    document.all.myapplet.style.visibility="visible";
    document.applets[0].repaint();
   }
   else
   {
    document.loading.visibility="hide";
    document.myapplet.visibility="visible";
   }
  }
</script>

Done. In fact, this approach also applies to using a picture to cover the applet until the applet starts to run. How to deal with, I think the smart you must already know the answer.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.