Publishers of technology books, eBooks, and videos for creative people

Home > Articles

  • Print
  • + Share This
This chapter is from the book

Checking if Java Is Enabled

Before you call on a Java applet to enhance your page, you should check to see if the user even has a Java-enabled browser. You could try to infer Java capability by the version of the browser, but it's not a reliable indicator because most browsers that can use Java also offer the option to turn it off. So you want to use code that asks the browser if Java is enabled (that means it is turned on). Script 3.6 shows you how to do it. As a side benefit, the script will also tell you if JavaScript is disabled.


Script 3.6 This script tells you whether the user has a Java-enabled browser.

To check if Java is enabled

  1. document.write("Java is ") As you can see, there's no time wasted here; you start by writing the beginning of a text string into the document window.
  2. if (!navigator.javaEnabled()) { This test statement uses the navigator.javaEnabled() method, which does what you expect: it asks the browser if Java is enabled. But here it is preceded by the ! operator, which means "not," so the statement asks "is Java not enabled?"
  3. document.write("not ") If the result of the test in step 2 is true (that is, Java is not enabled), then write the word "not " into the text string that is being built. Note the extra space after the word "not" which will ensure that the text string looks right.
  4. document.write("enabled.") Whether Java is enabled or not, we get to this point and finish the sentence, and write out the page, as shown in Figure 3.10 .

    Figure 3.10 This user's Java is hot and perking away.

  5. <noscript>
      JavaScript is not enabled.
    This is just plain HTML, but it is worth noting, as it tells the user that JavaScript isn't enabled.
  • + Share This
  • 🔖 Save To Your Account