By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
460,046 Members | 1,350 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 460,046 IT Pros & Developers. It's quick & easy.

JavaScript if statement not working

P: 5
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:

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. var test = "Is Java enabled? " + navigator.javaEnabled();
  5. var result = test.replace("Is Java enabled? ", "");
  6. if (result == "false") {document.getElementById("error").style.display = "";}
  7. if (result == "true") {document.getElementById("good").style.display = "";}
  8. </script>
  9. </head>
  10. <body>
  11. <div id="error" style="display:none;">
  12. <p>Java is NOT installed.</p>
  13. </div>
  14. <div id="good" style="display:none;">
  15. <p>Java is installed.</p>
  16. </div>
  17. </body>
  18. </html>
Mar 17 '12 #1
Share this Question
Share on Google+
10 Replies


Dormilich
Expert Mod 5K+
P: 8,639
yes, and?


that sure is some complicated code …
Mar 18 '12 #2

P: 5
Well, this code is not working. Does anyone know why?
Mar 18 '12 #3

Dormilich
Expert Mod 5K+
P: 8,639
have you checked what result contains?
Mar 18 '12 #4

P: 5
variable result contains "false" or "true". I checked using
Expand|Select|Wrap|Line Numbers
  1. document.write("result");
Mar 18 '12 #5

Dormilich
Expert Mod 5K+
P: 8,639
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"));
Mar 18 '12 #6

P: 5
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:
Expand|Select|Wrap|Line Numbers
  1. <script type="text/javascript">
  2. var test = "Is Java enabled? " + navigator.javaEnabled();
  3. var result = test.replace("Is Java enabled? ", "");
  4. if (result == "true") {document.write("TRUE");}
  5. if (result == "false") {document.write("FALSE");}
  6. </script>
Does that mean that the problem is in
Expand|Select|Wrap|Line Numbers
  1. document.getElementById(div_id).style.display = "";
?

P.S. I've checked it with document.write(result); -> I've mistyped it.
Mar 18 '12 #7

Dormilich
Expert Mod 5K+
P: 8,639
oh, there was a misconception on my side …

anyways, why not doing it the obvious way?
Expand|Select|Wrap|Line Numbers
  1. if (navigator.javaEnabled()) {
  2.     // Java enabled
  3. }
  4. else {
  5.     // Java not enabled
  6. }


Expand|Select|Wrap|Line Numbers
  1. 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")
Mar 18 '12 #8

P: 5
it still doesn't work....
Mar 18 '12 #9

Dormilich
Expert Mod 5K+
P: 8,639
comments indeed do nothing …
Mar 18 '12 #10

P: 9
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

Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script type="text/javascript">
  4. function java_test(){
  5. var objTrue = document.getElementById("good");
  6. var objFalse = document.getElementById("error");
  7. if(navigator.javaEnabled()){
  8. objTrue.style.visibility = "visible";
  9. }
  10. else{
  11. objFalse.style.visibility = "visible";
  12. }
  13. }
  14. </script>
  15. </head>
  16. <body>
  17. <div id="error" style="visibility:hidden">
  18. <p>Java is NOT installed.</p>
  19. </div>
  20. <div id="good" style="visibility:hidden">
  21. <p>Java is installed.</p>
  22. </div>
  23. <script type="text/javascript">
  24. java_test();
  25. </script>
  26. </body>
  27. </html>
  28.  
This, I believe, is what "Dormilich" meant by the simple way ;)
Apr 3 '12 #11

Post your reply

Sign in to post your reply or Sign up for a free account.