Home arrow JavaScript arrow Page 4 - JavaScript and Embedded Objects

JavaScript and Embedded Objects

There's more to Web browsers than (X)HTML, CSS, and JavaScript. ActiveX, plug-ins, and other embedded objects help make the Web what it is today. Get the scoop on these and more in this chapter excerpt from JavaScript: The Complete Reference, second edition, by Thomas Powell and Fritz Schneider McGraw-Hill/Osborne, ISBN 0072253576.

Author Info:
By: McGraw-Hill/Osborne
Rating: 4 stars4 stars4 stars4 stars4 stars / 92
October 26, 2004
  1. · JavaScript and Embedded Objects
  2. · Java
  3. · Including Applets
  4. · Accessing Applets in JavaScript
  5. · Issues with JavaScript-Driven Applets
  6. · Plug-ins
  7. · MIME Types
  8. · Detecting Specific Plug-Ins
  9. · Interacting with Plug-Ins
  10. · Refreshing the Plug-Ins Array
  11. · Interacting with a Specific Plug-In
  12. · ActiveX
  13. · Cross-Browser Inclusion of Embedded Objects
  14. · Interacting with ActiveX Controls
  15. · Summary

print this article

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>

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:

// or
// or the preferred access method

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;

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">
<title>Applet Interaction Example</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<script type="text/javascript">
var appletReady = false;
function changeMessage(newMessage) {
  if (!navigator.javaEnabled()) {
    alert("Sorry! Java isn't enabled!");
  if (appletReady)
    alert("Sorry! The applet hasn't finished loading");
// -->
<body onload="appletReady = true;">
<applet code="myhelloworld.class" width="400" height="100"
 name="myhelloworld" id="myhelloworld">
<em>Your browser does not support Java!</em>
<form action="#" method="get" onsubmit="return false;"
<input type="text" name="message" id="message" />
<input type="button" value="Change Message"
(document.inputForm.message.value);" />

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:


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.

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.

blog comments powered by Disqus

- Project Nashorn to Make Java, JavaScript Wor...
- JavaScript Virus Attacks Tumblr Blogs
- Google Releases Stable Dart Version, JavaScr...
- Khan Academy Unveils New JavaScript Learning...
- Accessing Nitro? There`s an App for That
- JQuery 2.0 Leaving Older IE Versions Behind
- Fastest JavaScript Engine Might Surprise You
- Microsoft Adjusting Chakra for IE 10
- Brendan Eich: We Don`t Need Google Native Cl...
- An Overview of JavaScript Statements
- An Overview of JavaScript Operators
- Overview of JavaScript Variables
- More of the Top jQuery Social Plugins
- The Top jQuery Social Plugins
- More of the Top jQuery Slider Plugins

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2019 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials