Which Browser is Running Me?

Which Browser is Running Me?

While Java’s ability to run on numerous computer platforms makes a developer’s task easier, it is sometimes necessary to customize code to match the abilities of the environment. A good example is the need to tailor the generation of Dynamic HTML syntax to the brand of browser which will be used to display it. Surprisingly, there seems to be no direct technique for determining the name of the program that invoked the Java Virtual Machine. The following method can be inserted in any class and will return a value suitable for use in a switch:

 // Gets run-time platform// Returns 0 for an Application//         1 for a Netscape  browser//         2 for a Microsoft browser//        -1 for an unknown platform    public int getPlatform() {        SecurityManager sm = System.getSecurityManager();        if (sm == null) return 0;        String st = sm.toString().toLowerCase();        if (st.startsWith("netscape")) return 1;        if (st.startsWith("" )) return 2;        return -1;    }


Share the Post: