Articles
DevASP - ASP and XML Articles, Samples, Toturials, Sample Chapters and resources for Developers Thursday, February 09, 2012
Home
Articles & Samples
Dev Search
Sample Chapters
Link to US
Contact
Search Directory
Applications
Articles & Samples
Components
Community
Database
Developer Sites
Downloads
Hosting Services
Introduction
Knowledge Base
Sample Chapters
WebCasts
ASP Directory
Applications
Articles & Samples
Components
Developer Sites
Knowledge Base
Sample Chapters
WebCasts
XML Directory
Applications
Articles & Samples
Developer Sites
Error, Bugs & Fixes
Downloads
Introduction
Knowledge Base
Sample Chapters
WebCasts

HTML 4.01 Programmer's Reference



Inserting a Java Applet with the Object Element
  With the <applet> element's deprecation in the HTML 4.01 standard, the way to embed Java applets is with the <object> element. It isn't quite as straightforward as all that. Java has a somewhat checkered history with web browsers. With its creation by Sun in the mid-1990s, the main browser vendors of the time (Netscape and Microsoft) were quick to realize its potential. They both quickly included versions of the Java Run-Time Environment within their browsers, which could be made use of by the <applet> element. However, Sun updated the Java environment on a regular basis, and so the browsers were left supporting out-of-date versions of the software.

While the creation of Opera didn't make much impact on the two main browsers, it did make the browser agent a much smaller entity, and was the first to consider making the Java Virtual Machine an optional add-on to the main browser. This has some ramifications now for putting Java applets in our web pages, because we still can't assume that every browser will automatically have Java installed (and even if we could some people would disable it anyway). Also, even if they did have it installed, it might not be the most up-to-date version, or a version recent enough to run the applet. It also means if we want to run Java applets on a web browser without native Java support, we must install the Java plug-in first via the codebase attribute. However, as downloading any component via the codebase attribute is a lengthy process, we suggest that if you really intend using Java (even if you're just browsing Java applets) then you should use a browser that contains the Java Virtual Machine.

Ok, we're going to borrow a very simple example Java Clock applet from the Sun Microsystems site, for which Sun holds the copyright, but the license allows it to be freely distributed. This is located at: http://java.sun.com/products/plugin/1.3/demos/applets/Clock/Clock2.java.

This should be saved as Clock2.java on your own machine, after which you need to use the JDK 1.3 to compile it as Clock2.class.

You can compile it using the command prompt/UNIX command line using the following code:
> javac Clock2.class

We're not interested in the internal workings of this code, so we're not going to explain how it works. It is enough to know that it will display a vector graphic clock on our web page. What we are interested in doing is embedding this applet in a web page, within the <object> element, so that it works in as many browsers as possible.

With Internet Explorer it's enough to supply the details in the classid attribute:

<object codetype="application/java"
        classid="java:Clock2.class" width="400" height="200">
  Your browser is ignoring the "object" element.
</object>

However it is possible to specify the latest version of the JDK as follows:

<object codetype="application/java;version=1.3"
        classid="java:Clock2.class" width="400" height="200">
  Your browser is ignoring the "object" element.
</object>

In which case we are dependent on having a version of IE with a recent enough JDK. In both cases it should result in something along the lines of:

However, for the browsers that don't include native Java support this won't be enough, and we will also need to indicate the location of a Java plug-in to be able to run Java applets. We need to specify the codebase as follows – this will be ignored if the browser already has native Java support. In IE versions without the Java SDK the code would be as follows:

<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="300"
        height="200" codebase="http://java.sun.com/products/
        plugin/1.3/jinstall-13-win32.cab#Version=1,2,0,0">
  <param name="code" value="Clock2.class">
  <param name="type" value="application/x-java-applet;version=1.3">
  Your browser is ignoring the "object" element.
</object>

However, the .cab executable specified is something unique to IE, and to get it work in Netscape 6 we actually need to specify the Sun Java plug-in instead:

<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" width="300"
        height="200" codebase="http://java.sun.com/products/plugin/
        1.3/plugin-install.html">
  <param name="code" value="Clock2.class">
  <param name="type" value="application/x-java-applet;version=1.3">
  Your browser is ignoring the "object" element.
</object>

Here we've used a version of Netscape 6, without the Java SDK included, and the output will look like this:

Of course this still leaves the problem of what to do with browsers that don't support the <object> element entirely, such as Netscape 4. In such cases, we can tuck the <applet> tag into the code as follows, making sure to comment it out (for Internet Explorer):

<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
        WIDTH=300 height=200 codebase="http://java.sun.com/products/
        plugin/1.3/jinstall-13-win32.cab#Version=1,2,0,0">
  <param name="code" value="Clock2.class">
  <param name="type" value="application/x-java-applet;version=1.3">
  <comment>
    <applet code="Clock2.class" width="170" height="150">
    </applet>
  </comment>
</object>

The IE-specific <comment> tag is the only one available, and if it isn't used, then both the <object> and the <applet> elements are interpreted by IE, and the applet will be displayed twice. This is direct contradiction to what we said earlier about objects; the expected behavior should be to ignore the <applet> element. However, with the <applet> element in IE, there seems to be this anomaly, which requires the use of the <comment> tag. There is more on the <applet> element in the Chapter 15.

Inserting a Flash Animation with the Object Element
  As mentioned earlier in this chapter, the way to insert a Flash animation into our web page is with the <object> element. Normally with Flash, we'd use the publishing tool (accessed in Flash via the File | Publish menu) to create the HTML for us, and so we wouldn't have to get involved with actual coding, as the publishing tool does this all automatically for us. However there are times when we might want to manually place a Flash animation into our web pages.

It's also worth mentioning that the code created by the publishing tool uses <object> to insert the animation in Internet Explorer, and within the <object> element, there is an <embed> element to insert the animation so that the Netscape 4 and Opera browsers can run it as well. The Netscape 6 browser seems unable to execute it via the <object> element – once again Microsoft .cab files are required to run the Flash tool within <object>, and so Netscape 6 relies on the <embed> element. Now let's look at an example.

We've borrowed a Flash file (savior.swf – see the code download) courtesy of Friends of Ed (http://www.friendsofed.com), which allows us to play a simple arcade game. Once again the code that creates the embedded application is not important, just the method by which we are inserting it. Flash is now commonly included in the latest browsers, but to be on the safe side we've added a codebase attribute to point to the flash download screen.

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
        codebase="http://download.macromedia.com/pub/shockwave/
        cabs/flash/swflash.cab#version=5,0,0,0" width="500"
        height="400">
  <param name="movie" value="savior.swf">
  <param name="quality" value="high">
  <param name="bgcolor" value="#FFFFFF">
  Flash animations not supported with "object"
</object>

Assuming the savior.swf file is located in the same directory as the web page, which has the above information included, we should see the following in Internet Explorer:

To get this animation to work in Netscape and Opera, we need to enclose the following <embed> element code with the <object> element code:

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000"
        codebase="http://download.macromedia.com/pub/shockwave/
        cabs/flash/swflash.cab#version=5,0,0,0" width=500
        height=400>
  <param name="movie" value="savior.swf">
  <param name="quality" value="high">
  <param name="bgcolor" value="#000000">
  <embed src="savior.swf" quality="high" bgcolor="#000000" width=550
         height="400" type="application/x-shockwave-flash"
         pluginspace="http://www.macromedia.com/shockwave/download/
         index.cgi?P1_Prod_Version=ShockwaveFlash">
  </embed>
</object>

The <embed> element is not even deprecated as it never formed part of any HTML standard, although IE, Netscape, and Opera all support it. The pluginspace attribute is equivalent to the IE codebase attribute. There is more on the <embed> element in Chapter 16. One last thing to notice is that we don't require the <comment> tag here, as IE will only embed one occurrence of the Flash animation here (as it is meant to), despite the fact that technically there are two specified, because it ignores what is inside an <object> element, as it should if it handles the tag itself.

Inserting an Excel Spreadsheet
  It is also possible to insert simple items in Internet Explorer, such as spreadsheets and Word documents. We could use a tool such as FrontPage to do the basic work for us, but all FrontPage is actually doing is creating the requisite <object> element and inserting the necessary data as param attributes. The following code would insert a blank Excel spreadsheet into our web page.
<object classid="CLSID:0002E510-0000-0000-C000-000000000046">
  <param name="DisplayTitleBar" value="false">
</object>

This unfortunately won't produce anything on Netscape or Opera, as they don't support COM objects.

Including HTML in Another HTML Document
  The HTML 4.01 standard also allows us to use the <object> element to "insert HTML documents into our web pages". Inserting web pages into web pages might seem like a pointless pastime, but there are two reasons why it might be very useful:
  • First, it acts as an alternative to the <iframe> element, which is good, given that <iframe> isn't officially part of the HTML 4.01 standard. However, there are problems with its use for this purpose. An <iframe> element can be targeted by a link or form, and can be focused and printed, while an <object> element doesn't support these features.
  • Secondly, it enables us to easily reuse a single section of code (either HTML or script) many times throughout a site, without having to reproduce the code in its entirety.

We can embed a document using the data attribute as follows:

<object data="embedded_document.htm">
  Couldn't include document specified.
</object>

However, there has been a security hole associated with this ability in IE 5.5. It allows the execution of arbitrary programs using object type="text/html" and parsing index.dat, by revealing the location of the temporary internet files folder, which could possibly lead to a hacker taking full control over user's computer. There is a patch available to rectify this. Details of this security issue can be found at http://www.microsoft.com/technet/security/bulletin/MS00-055.asp, and details of the patch at http://www.microsoft.com/windows/ie/download/critical/patch11.htm. The patch basically stops this from working. It also doesn't work in Netscape. The only surefire browser to support this is Opera 5.x.

Microsoft also provides the ability to embed script code within HTML documents as Behaviors. Behaviors are platform-independent components that can be deployed on the Web. Formerly their predecessor (known as Scriptlets) used the <object> element to achieve this. However, this was very much an evolving technology, and it proved to be a flawed way of inserting them, so Behaviors are now embedded using style sheets instead. As a result this is very much a technology beyond the scope of this book and one we won't discuss any further.

Summary In this chapter, we've introduced and explained how to add objects to our web pages. We gave a brief overview of the <object> element. We introduced the different attributes and showed how it could be used to add images to HTML documents, as well as objects such as videos and MP3 files. This is the method that is recommended by the HTML 4.01 standard, and the method that should be used, if possible.

We rounded off the chapter with an overview of how we can embed some of the most popular components within our web pages. However, we suggested that as Internet Explorer doesn't properly support the <object> element for images, it is unlikely to displace the <img> element in popular usage, but for all other types of media, it is very useful.

<<<<.....back
DevASP - Privacy - Disclaimer
Copyright © 2008 DevASP.com