Sun Microsystems, Inc.
spacerspacer
spacer www.sun.com docs.sun.com |
spacer
black dot
 
 
3.  Using OBJECT, EMBED and APPLET Tags in Java Plug-in Introduction Java Plug-in in Internet Explorer and Netscape Navigator Browsers  Previous   Contents   Next 
   
 

This example illustrates that you can use the combined OBJECT-EMBED tag to activate Java Plug-in in the browser if either Internet Explorer or a Netscape Navigator browser is used. This combined tag is strongly recommended unless your HTML page is browsed by users in a homogeneous environment. The Java Plug-in HTML Converter from Sun Microsystems automatically converts HTML pages into this tag style for you.

Java Plug-in Anywhere

In an Internet/intranet environment, an HTML page is likely to be seen by browsers on different platforms. You should activate Java Plug-in only on the right browser and platform combination. Otherwise, you should use the browser's default JVM. You can achieve this using the following Java Plug-in tag:

Original APPLET tag:

<APPLET code="XYZApp.class" codebase="html/" align="baseline"
          width="200" height="200">
<PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz">
          No Java 2 SDK, Standard Edition v 1.4 support for APPLET!!
</APPLET>

New style tag:

The following is an example of an equivalent Java Plug-in tag. This example includes comments.

<!-- The following code is specified at the beginning of the <BODY> tag. -->
<SCRIPT LANGUAGE="JavaScript"><!--
 var _info = navigator.userAgent; var _ns = false;
      var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0
          && _info.indexOf("Windows 3.1") < 0);
  //--></SCRIPT>
<COMMENT><SCRIPT LANGUAGE="JavaScript1.1"><!--
var _ns = (navigator.appName.indexOf("Netscape") >= 0
    && ((_info.indexOf("Win") > 0 && _info.indexOf("Win16") < 0
    && java.lang.System.getProperty("os.version").indexOf("3.5") < 0)
    || _info.indexOf("Sun") > 0));
  //--></SCRIPT></COMMENT>

<!-- The following code is repeated for each APPLET tag -->
<SCRIPT LANGUAGE="JavaScript"><!--
      if (_ie == true) document.writeln('
<OBJECT
classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93"
width="200" height="200" align="baseline"
codebase="http://java.sun.com/jpi/jinstall-14-win32.cab#Version=1,4,0,mn">
      <NOEMBED><XMP>');
      else if (_ns == true) document.writeln('
<EMBED
      type="application/x-java-applet;jpi-version=1.4" 
      width="200" height="200"
      align="baseline" code="XYZApp.class" codebase="html/"
      model="models/HyaluronicAcid.xyz"
      pluginspage="http://java.sun.com/jpi/plugin-install.html">
      <NOEMBED><XMP>');
//--></SCRIPT>
      <APPLET code="XYZApp.class" codebase="html/" align="baseline"
          width="200" height="200">
</XMP>
<PARAM NAME="java_code" VALUE="XYZApp.class">
<PARAM NAME="java_codebase" VALUE="html/">
<PARAM NAME="java_type" VALUE="application/x-java-applet;jpi-version=1.4">
<PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz">
<PARAM NAME="scriptable" VALUE="true">
      No Java 2 SDK, Standard Edition v 1.4 support for APPLET!!
</APPLET></NOEMBED></EMBED>
</OBJECT>

<!--
      <APPLET code="XYZApp.class" codebase="html/" align="baseline"
          width="200" height="200">
      <PARAM NAME="model" VALUE="models/HyaluronicAcid.xyz">
      No Java 2 SDK, Standard Edition v 1.4 support for APPLET!!
      </APPLET>
-->

Although this tag seems complicated compared to the old APPLET tag, it is not. Most of the Java Plug-in tag is the same regardless of the applet used. For the majority of cases, a webmaster can copy and paste the Java Plug-in tag.

The first block of the script extracts the browser and platform. You must determine the browser and platform on which the applet is running. You do this by using JavaScript™ to extract first the browser name, then the platform. This is done once per HTML document.

The second block of the script replaces the APPLET tag. You must replace each APPLET tag with a similar block of code. The script replaces the APPLET tag with either an EMBED tag or OBJECT tag, depending on the browser. You use the OBJECT tag for Internet Explorer and the EMBED tag for Netscape Navigator browsers. Finally, the original APPLET tag is included as a comment at the end. It is always a good idea to keep the original APPLET tag in case you want to remove the Java Plug-in invocation.

The first JavaScript establishes the browser and the platform on which the browser is running. You must do this because, currently, Java Plug-in supports only Microsoft Windows platforms and the Solaris operating environment. Note that Windows NT 3.51 is the only Win32 platform that Java Plug-in does not support. Java Plug-in should be invoked only on the supported browser and platform. The script sets the variable _ie to true if the browser is Internet Explorer. It sets the variable _ns to true if the browser is a Netscape browser. (Note that all variable names in the JavaScript start with "_". This is done to avoid conflicting with other JavaScript variables in the same page.)

To detect the right browser, the JavaScript evaluates three strings that are within the JavaScript's Navigator object: userAgent, appVersion, and appName. These strings contain information about the browser and the platform. By looking at some examples of the string userAgent, you can see how to evaluate userAgent and use it to determine the browser. The following are some examples of the userAgent string for different platforms as it appears in Internet Explorer. In the table, IE stands for Internet Explorer.

Platform and Browser

JavaScript's Navigator.userAgent string

Windows 2000 w/IE 5.0

"Mozilla/4.0 (compatible; MSIE 5.01; Window NT5.0)

Windows NT 4.0 w/ IE 4.0

"Mozilla/4.0 (compatible; MSIE 4.0; Windows NT)" "

Windows 95 w/IE 4.0

"Mozilla/4.0 (compatible; MSIE 4.0; Windows 95)"

Windows NT 3.51 w/ IE 4.0

"Mozilla/4.0 (compatible; MSIE 4.0; Windows 3.1)"

Windows 3.1 w/IE 4.0

"Mozilla/4.0 (compatible; MSIE 4.0; Windows 3.1)"

Windows NT 4.0 w/Navigator 4.04

"Mozilla/4.04 [en] (WinNT; I)"

Windows NT 3.51 w/Navigator 4.04

"Mozilla/4.04 [en] (WinNT; I)"

Windows 95 w/Navigator 4.03

"Mozilla/4.03 [en] (Win95; I)"

Solaris 2.6 w/Navigator 4.02

"Mozilla/4.02 [en] (X11; l; SunOS 5.6 sun4u)

Note that in each case the substring "MSIE" is always in the userAgent string in Internet Explorer. Also, the userAgent string in Internet Explorer under Windows 3.1 and Windows 3.51 would contain the substring "Windows 3.1" because Internet Explorer in these platforms is 16-bit. While Internet Explorer 4 is also available on Macintosh and the Solaris operating environment, in these versions the userAgent string does not contain the substring "Win". In addition, Internet Explorer on Windows CE does not support JavaScript. This can be summarized as follows:

userAgent string / Browser

contains "MSIE"

contains "Win"

does not contain "Windows 3.1"

Windows 3.1 w/ IE 4

X

X

 

Windows NT 3.51 w/ IE 4

X

X

 

Windows 95 w/IE 4

X

X

X

Windows NT 4.0 w/ IE 4

X

X

X

Windows CE w/ IE

 

 

 

Mac w/ IE

X

 

X

UNIX w/ IE

X

 

X

Other browsers on any platform

 

 

X

The above table shows that only Windows 95 and Windows NT 4.0 with Internet Explorer can pass the Java Plug-in browser and platform requirements. However, this logic makes no assumptions about future releases of Internet Explorer or future releases of Microsoft Windows with Internet Explorer. As long as the userAgent string contains "MSIE" and "Win", the above code should work in future releases of Internet Explorer on Win32.

The above logic summarizes into the following:

<SCRIPT LANGUAGE="JavaScript">
          var _info = navigator.userAgent;
          var _ie = (_info.indexOf("MSIE") > 0 && _info.indexOf("Win") > 0
                  && _info.indexOf("Windows 3.1") < 0);
//--></SCRIPT>

It is harder to detect Navigator on the right platform. Using just JavaScript, there is no way to determine if the browser is running on the Windows NT 3.51 or Windows NT 4.0 operating platform. (Refer to the above table and examine the userAgent string. Notice that the userAgent strings in Windows NT 3.51 and Windows NT 4.0 operating platforms are the same in Netscape Navigator browsers.) It is important to make this distinction because Java Plug-in supports only the Windows NT 4.0 operating platform. To run Java Plug-in on the right platform, you must use LiveConnect in Netscape Navigator browsers to determine the OS version number. This can be summarized as follows:

Testing logic / Browser

appName contains "Netscape"

userAgent contains "Win"

userAgent does not contain "Win16"

os.version does not contain 3.5

userAgent contains "Sun"

Windows 3.1 w/ Netscape 4

X

X

 

X

 

Windows NT 3.51 w/ Netscape 4

X

X

X

 

 

Windows 95 w/ Netscape 4

X

X

X

X

 

Windows NT 4.0 w/ NS 4

X

X

X

X

 

NS on Solaris

X

 

 

 

X

IE on Solaris

 

 

 

 

X

Netscape on other platform

X

 

X

Depends on OS

 

IE on other platform

 

X

X

 

 

Other browsers on any platform

 

 

 

 

 

The above logic translates into the following code:

<SCRIPT LANGUAGE="JavaScript">
         var _info = navigator.userAgent; var _ns = false;
//--></SCRIPT>
<COMMENT><SCRIPT LANGUAGE="JavaScript1.1">
    var _ns = (navigator.appName.indexOf("Netscape") >= 0
        && ((_info.indexOf("Win") > 0  && _info.indexOf("Win16") < 0
        && java.lang.System.getProperty("os.version").indexOf("3.5") < 0)
        || _info.indexOf("Sun") > 0));
//--></SCRIPT></COMMENT>
 
 
 
  Previous   Contents   Next