Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Intermediate
May 29, 1998

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("com.ms." )) return 2;
        return -1;
    }
Tom Yarker
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap