JavaScript if statement not working

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Nick Vinter
    New Member
    • Mar 2012
    • 5

    JavaScript if statement not working

    Hello.
    I'm trying to make a javascript that detects if user has Java, on then is he does show one div, e.g. "<div id='error'>" of "<div id='good'>" if he does have Java installed.
    Here is the script:

    Code:
    <html>
    <head>
    <script type="text/javascript">
    var test = "Is Java enabled? " + navigator.javaEnabled();
    var result = test.replace("Is Java enabled? ", "");
    if (result == "false") {document.getElementById("error").style.display = "";}
    if (result == "true") {document.getElementById("good").style.display = "";}
    </script>
    </head>
    <body>
    <div id="error" style="display:none;">
    <p>Java is NOT installed.</p>
    </div>
    <div id="good" style="display:none;">
    <p>Java is installed.</p>
    </div>
    </body>
    </html>
    Last edited by Dormilich; Mar 18 '12, 01:23 AM. Reason: Please use [CODE] [/CODE] tags when posting code.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    yes, and?


    that sure is some complicated code …

    Comment

    • Nick Vinter
      New Member
      • Mar 2012
      • 5

      #3
      Well, this code is not working. Does anyone know why?

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        have you checked what result contains?

        Comment

        • Nick Vinter
          New Member
          • Mar 2012
          • 5

          #5
          variable result contains "false" or "true". I checked using
          Code:
          document.write("result");

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            I doubt that. the above code would print "result", not true/false. besides that, while true/false may print as "true"/"false", they do not equal that, i.e. (false == "false") would evaluate as false. simply check with alert((true == "false"));

            Comment

            • Nick Vinter
              New Member
              • Mar 2012
              • 5

              #7
              I've checked it with alert((true == "false")); -> message was false
              If I change the script to following, this will work.
              Here is the new test code:
              Code:
              <script type="text/javascript">
              var test = "Is Java enabled? " + navigator.javaEnabled();
              var result = test.replace("Is Java enabled? ", "");
              if (result == "true") {document.write("TRUE");}
              if (result == "false") {document.write("FALSE");}
              </script>
              Does that mean that the problem is in
              Code:
              document.getElementById(div_id).style.display = "";
              ?

              P.S. I've checked it with document.write( result); -> I've mistyped it.

              Comment

              • Dormilich
                Recognized Expert Expert
                • Aug 2008
                • 8694

                #8
                oh, there was a misconception on my side …

                anyways, why not doing it the obvious way?
                Code:
                if (navigator.javaEnabled()) {
                    // Java enabled
                }
                else {
                    // Java not enabled
                }


                Code:
                document.getElementById(div_id).style.display = "";
                that resets it to the default value, which I think is the one you give it in the style declaration (which is "none")

                Comment

                • Nick Vinter
                  New Member
                  • Mar 2012
                  • 5

                  #9
                  it still doesn't work....

                  Comment

                  • Dormilich
                    Recognized Expert Expert
                    • Aug 2008
                    • 8694

                    #10
                    comments indeed do nothing …

                    Comment

                    • wbevan20
                      New Member
                      • Feb 2012
                      • 9

                      #11
                      The problem seems to be that the script was working before the document body was being processed, therefore the call to show the div tags was, essentially ignored because at that stage they were already visible.

                      I have made a version of your page/script that should work correctly

                      Code:
                      <html>
                      <head>
                      <script type="text/javascript">
                      function java_test(){
                      var objTrue = document.getElementById("good");
                      var objFalse = document.getElementById("error");
                      if(navigator.javaEnabled()){
                      objTrue.style.visibility = "visible";
                      }
                      else{
                      objFalse.style.visibility = "visible";
                      }
                      }
                      </script>
                      </head>
                      <body>
                      <div id="error" style="visibility:hidden">
                      <p>Java is NOT installed.</p>
                      </div>
                      <div id="good" style="visibility:hidden">
                      <p>Java is installed.</p>
                      </div>
                      <script type="text/javascript">
                      java_test();
                      </script>
                      </body>
                      </html>
                      This, I believe, is what "Dormilich" meant by the simple way ;)

                      Comment

                      Working...