EarthWeb
Developer.com
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Book
thinking in java
 
Interact
forum
guest book
jobs
jokes
what's new

share code
 
Resource
add resource
modify resource
new resource
 

[Internet Jobs]
-----
Java by E-mail:

Get the weekly e-mail highlights on Java!
-----

-

Detect the browser/JVM type


Author: Real Gagnon
Author's WebSite: http://tactika.com/realhome/realhome.html

One way is to instanciate a known browser-specific method and catch the Exception if not found


 import java.applet.*;

 public class BrowserDetector extends Applet {
   public void init() {
     if ( isNetscape() ) 
        System.out.println("This browser is a Netscape Browser.");
     if ( isMicrosoft() )
        System.out.println("This browser is a Microsoft Browser.");
     }
     
   public static boolean isNetscape() {
     try {
       Class.forName("netscape.applet.MozillaAppletContext");
       } 
     catch (ClassNotFoundException e) { 
       System.out.println("This browser is not a Netscape Browser.");
       return false; 
       }
     return true;
     }    

   public static boolean isMicrosoft() {
     try {
       Class.forName("com.ms.applet.GenericAppletContext");
       }
     catch (ClassNotFoundException e) {
       System.out.println("This browser is not a Microsoft Browser.");
       return false;
       }    
     return true;
     }    
 }



Or by examining the string representation of the getAppletContext() method


 String theBrowser = "APPLICATION";
 String appletContext = getAppletContext().toString();
 if (appletContext.startsWith("sun.applet.AppletViewer"))
    theBrowser = "APPLETVIEWER";
 else if (appletContext.startsWith("netscape.applet."))
    theBrowser = "NETSCAPE";
 else if (appletContext.startsWith("com.ms.applet."))
    theBrowser = "MICROSOFT";
 else if (appletContext.startsWith("sunw.hotjava.tags.TagAppletPanel"))
    theBrowser = "HOTJAVA";
 else if (appletContext.startsWith( "sun.plugin.navig.win32.AppletPlugin"))
    theBrowser = "NETSCAPEPLUGIN";
 else if (appletContext.startsWith( "sun.plugin.ocx.ActiveXApplet"))
    theBrowser = "MICROSOFTPLUGIN;



For an application, by looking at the string representation of the getDefaultToolkit() method, we detect the JVM type


 String theJVM = "";
 String toolkit = Toolkit.getDefaultToolkit().toString();

 if (theBrowser.equals("APPLICATION") {
    if (toolkit.startsWith( "sun.awt.windows.WToolkit"))
       theJVM = "JAVA";
    else if (toolkit.startsWith( "com.ms.awt.WToolkit"))
       theJVM = "JVIEW";
    }



For example, our MyApplet.class exists in three versions. One is using Microsoft-specific classes, the other is a JDK1.1 applet and finally a version for JDK102-only browser. The idea is to put all the required classes in an ARCHIVE file. By using a javascript entities, we decide which archive to use. During layout time, the javascript entity is remplaced by the right archive name.


<HTML></HTML><HEAD>
<SCRIPT>

  function isBrowser(b,v) {
     browserOk = false;
     versionOk = false;
     browserOk = (navigator.appName.indexOf(b) != -1);
     versionOk = (v <= parseInt(navigator.appVersion));
     return browserOk && versionOk;
    }
  
  archiveToBeUsed = "java102.jar";

  if (isBrowser("Microsoft", 4)) {
     archiveToBeUsed = "ie4.jar";
     }
  else {
     if isBrowser("Netscape", 4) {
        archiveToBeUsed = "n4.jar";
        }
     }



</SCRIPT></HEAD><BODY>
<APPLET CODE ="MyApplet.class"
HEIGHT=100
WIDTH=400
ARCHIVE=&{archiveToBeUsed}; >
</APPLET>
</BODY></HTML>


Posted On: 7-Jul-1999

internet.commerce



Acceptable Use Policy

JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Web Hosting | Newsletters | Tech Jobs | Shopping | E-mail Offers