JavaScript
  Home arrow JavaScript arrow Page 4 - JavaScript and Embedded Objects
Web Buyers Guide
CIO Insight
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Dedicated Servers  
Download TestComplete 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

JavaScript and Embedded Objects
By: McGraw-Hill/Osborne
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 43
    2004-10-26

    Table of Contents:
  • JavaScript and Embedded Objects
  • Java
  • Including Applets
  • Accessing Applets in JavaScript
  • Issues with JavaScript-Driven Applets
  • Plug-ins
  • MIME Types
  • Detecting Specific Plug-Ins
  • Interacting with Plug-Ins
  • Refreshing the Plug-Ins Array
  • Interacting with a Specific Plug-In
  • ActiveX
  • Cross-Browser Inclusion of Embedded Objects
  • Interacting with ActiveX Controls
  • Summary

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
     
    ADVERTISEMENT

    Free Web 2.0 Code Generator! Generate data entry and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    JavaScript and Embedded Objects - Accessing Applets in JavaScript
    (Page 4 of 15 )

    The ability to communicate with applets originated with a Netscape technology called LiveConnect that was built into Netscape 3. This technology allows JavaScript, Java, and plug-ins to interact in a coherent manner and automatically handles type conversion of data to a form appropriate to each. Microsoft implemented the same capabilities in IE4, though not under the name LiveConnect. The low-level details of how embedded objects and JavaScript interact are complicated, unique to each browser, and even vary between different versions of the same browser. The important thing is that no matter what it is called, the capability exists in versions of IE4+ (except under Macintosh) and Netscape 3+ (although early versions of Netscape 6 have some problems), and Mozilla-based browsers.

    Applets can be accessed through the applets[] array of the Document object or directly through Document using the applet’s name. Consider the following HTML:

    <applet code="myhelloworld.class" width="400" height="100"
    name="myhelloworld" id="myhelloworld">
    <em>Your browser does not support Java!</em>
    </applet>

    Assuming that this applet is the first to be defined in the document, it can be accessed in all of the following ways, with the last being preferred:

    document.applets[0]
    // or
    document.applets["myhelloworld"]
    // or the preferred access method
    document.myhelloworld

    The JavaScript properties, defined primarily under the browser object model and later by the DOM, of an Applet object are listed in Appendix B and consist of an unsurprising assortment of information reflecting the attributes of the (X)HTML <applet> tag for which it was defined. The relevant aspect to this JavaScript-Java communication discussion is the fact that all properties and methods of the applet’s class that are declared public are also available through the Applet object. Consider the following Java class definition for the previous myhelloworld example. The output (when embedded as before) is shown in Figure 18-1.

    import java.applet.Applet;
    import java.awt.Graphics;
    public class myhelloworld extends Applet
    {
      String message;
      public void init()
      {
        message = new String("Hello browser world from Java!");
      }
      public void paint(Graphics myScreen)
      {
        myScreen.drawString(message, 25, 25);
      }
        public void setMessage(String newMessage)
      {
      message = newMessage;
      repaint();
      }
    }


    FIGURE 18-1
    The output of the myhelloworld applet in Internet Explorer

    Now comes the interesting part. Because the setMessage() method of the myhelloworld class is declared public, it is made available in the appropriate Applet object. We can invoke it in JavaScript as

    document.myhelloworld.setMessage("Wow. Check out this new message!");

    Before proceeding further with this example, it is very important to note that applets often require a significant amount of load time. Not only must the browser download the required code, but it also has to start the Java virtual machine and walk the applet through several initialization phases in preparation for execution. It is for this reason that it is never a good idea to access an applet with JavaScript before making sure that it has begun execution. The best approach is to use an onload handler for the Document object to indicate that the applet has loaded. Because this handler fires only when the document has completed loading, you can use it to set a flag indicating that the applet is ready for interaction. This technique is illustrated in the following example using the previously defined myhelloworld applet:

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <title>Applet Interaction Example</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    </head>
    <script type="text/javascript">
    <!--
    var appletReady = false;
    function changeMessage(newMessage) {
      if (!navigator.javaEnabled()) {
        alert("Sorry! Java isn't enabled!");
        return;
      }
      if (appletReady)
        document.myhelloworld.setMessage(newMessage);
      else
        alert("Sorry! The applet hasn't finished loading");
    }
    // -->
    </script>
    <body onload="appletReady = true;">
    <applet code="myhelloworld.class" width="400" height="100"
     name="myhelloworld" id="myhelloworld">
    <em>Your browser does not support Java!</em>
    </applet>
    <form action="#" method="get" onsubmit="return false;"
     name="inputForm"
    id="inputForm">
    <input type="text" name="message" id="message" />
    <input type="button" value="Change Message"
     onclick="changeMessage
    (document.inputForm.message.value);" />
    </form>
    </body>
    </html>

    The output of this script after changing the message is shown in Figure 18-2. There are tremendous possibilities with this capability. If class instance variables are declared public, they can be set or retrieved as you would expect:

    document.appletName.variableName

    Inherited variables are, of course, also available.


    Note Java applets associated with applets defined in <object> tags receive the public properties and methods just as those defined in <applet> tags do. However, using <object> instead of <applet> is potentially less cross-browser compatible because Netscape 4 does not expose this HTML element to scripts.


    FIGURE 18-2
    JavaScript can call public methods of Java applets.

    McGraw-Hill-OsborneThis chapter is from JavaScript: The Complete Reference, second edition, by Thomas Powell and Fritz Schneider, McGraw-Hill/Osborne, ISBN: 0072253576). Check it out at your favorite bookstore today.

    Buy this book now.

    More JavaScript Articles
    More By McGraw-Hill/Osborne


     

    JAVASCRIPT ARTICLES

    - A Closer Look at Smart Markers with Yahoo! M...
    - Using Polylines and Smart Markers with Yahoo...
    - Bulleted Menu of Links
    - Creating Click Loggers and Basic Markers wit...
    - Adding Pan Controls to Yahoo! Maps
    - Adding Zoom Controls to Yahoo! Maps
    - Working with Yahoo! Maps
    - Building Image Zooming Controls with the DOM...
    - Working with Multiple Graphics for a Zoom Ap...
    - Improving an Image Zooming Application with ...
    - Zooming in on Images with JavaScript
    - JavaScript Date Objects: Universal Coordinat...
    - Javascript Objects: More Date Methods
    - JavaScript Objects: Dates
    - JavaScript Objects: Finishing Strings


     
    Accelerating Trading Partner Performance
     
    Competing on Analytics
     
    Cost Effective Scaling with Virtualization and Coyote Point Systems
     
    Five Checkpoints to Implementing IP Telephony
     
    Hosted Email Security: Staying Ahead of New Threats
     





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway