JAVASCRIPT: Display code based on java being enabled

Posted on 2007-10-02
Last Modified: 2012-05-05

I have a page where I check to see if java is enabled using javascript:

<script language="JavaScript">
var Jcheck = navigator.javaEnabled();

I then use that condition to see if it is true or false:

if (Jcheck != "true")   {
                  var appName = "!--"
else                  { var appName = "applet"      


document.write("<" + appName + "" );
If it is true then I want to write out the code to display an applet.

I thought maybe I could comment out the first part of the applet code if JAVA is NOT enabled but this did not work. Is there a way to display the applet code if java is enabled but hide it and display a message if java isn't enabled?

Any assistance would be appreciated!


Here is a sample of the applet code:

<applet name="My applet"
      width=400 height=250>
      <param name=NFParamScript value='

Background   = (,BOX,1,"null",TILE,black);
LabelPos     = 0.7;
DwellLabel   = ("",black,"Courier",16,0);
DwellBox     = (white,SHADOW,3,null,TILE,black);
Slices       = (35,orange,"Oranges",black,"Arial",14,0,null,NONE,4,"null",TILE,black),(65,red,"Apples",black,"Arial",14,0,null,NONE,4,"null",TILE,black);
ActiveLabels = (),();
SlicePos     = 0.0,0.0;
Legend                = ("Legend",black,"Helvetica",10,0);
LegendAxis            = (BOTTOM,LEFT);
LegendLayout          = (VERTICAL,RIGHT,0,0,TOPLEFT,-1);
LegendBox             = (null,NONE,1,null,TILE,black);
Question by:baxleyb
    LVL 17

    Accepted Solution

    I would simply place a div in your document with a default text:
    <div id="myJavaContainer">
      <p>This website needs to use Java and JavaScript technology. One, or both, of these technologies has not been enabled on your system.</p>

    <script type="text/javascript">
    if (navigator.javaEnabled()) {
      var container = document.getElementById("myJavaContainer");
      if (container) container.innerHTML = "<applet name=\"My applet\"\n" +
    "      code=bla\n" +
    "      codebase=./classes\n" +
    "      archive=bla.jar\n" +
    "      width=400 height=250>\n" +
    "      <param name=NFParamScript value='\n" +
    "\n" +
    "Background   = (,BOX,1,\"null\",TILE,black);\n" +
    "LabelPos     = 0.7;\n" +
    "DwellLabel   = (\"\",black,\"Courier\",16,0);\n" +
    "DwellBox     = (white,SHADOW,3,null,TILE,black);\n" +
    "Slices       = (35,orange,\"Oranges\",black,\"Arial\",14,0,null,NONE,4,\"null\",TILE,black),(65,red,\"Apples\",black,\"Arial\",14,0,null,NONE,4,\"null\",TILE,black);\n" +
    "ActiveLabels = (),();\n" +
    "SlicePos     = 0.0,0.0;\n" +
    "Legend                = (\"Legend\",black,\"Helvetica\",10,0);\n" +
    "LegendAxis            = (BOTTOM,LEFT);\n" +
    "LegendLayout          = (VERTICAL,RIGHT,0,0,TOPLEFT,-1);\n" +
    "LegendBox             = (null,NONE,1,null,TILE,black);\n" +
    "'>\n" +

    This would replace the default text with your applet when java is enabled.

    Author Comment

    Perfect. Thanks for a simple and effective solution.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Course: HTML5 for Beginning Beginners

    Are you looking for an efficient introduction to HTML5? Have you tried other HTML5 courses and found them too complex? Do you need just the basics of HTML5 to use with Wordpress or other content management system? If you said yes to any of these questions, this course is for you.

    Suggested Solutions

    Article by: DanRollins
    This article describes a JavaScript program that creates a maze made of hexagonal cells.  In Part 2 (, we'll extend the program by adding a depth-…
    International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
    The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
    The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now