JAVASCRIPT: Display code based on java being enabled


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);
Who is Participating?
mreuringConnect With a Mentor Commented:
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.
baxleybAuthor Commented:
Perfect. Thanks for a simple and effective solution.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.