473,320 Members | 1,947 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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:

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
10 2167
Dormilich
8,658 Expert Mod 8TB
yes, and?


that sure is some complicated code …
Mar 18 '12 #2
Well, this code is not working. Does anyone know why?
Mar 18 '12 #3
Dormilich
8,658 Expert Mod 8TB
have you checked what result contains?
Mar 18 '12 #4
variable result contains "false" or "true". I checked using
Expand|Select|Wrap|Line Numbers
  1. document.write("result");
Mar 18 '12 #5
Dormilich
8,658 Expert Mod 8TB
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
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
8,658 Expert Mod 8TB
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
it still doesn't work....
Mar 18 '12 #9
Dormilich
8,658 Expert Mod 8TB
comments indeed do nothing …
Mar 18 '12 #10
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

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

Similar topics

6
by: Web Spinner | last post by:
I am running Windows 2000 and I can't seem to get my JavaScript working with ASP. I placed my HTML code in a file called x.asp under c:\Inetpub\wwwroot, and I placed my JavaScript code in a file...
2
by: Kamyk | last post by:
Hello all! Could you tell me where is the error on the below code, because the script is sometimes working correctly and sometimes is not working correctly. I want my new window with picture...
4
by: web_design | last post by:
I put this together from some other scripts I am using on a site. I'm trying to make a better email hiding script. It isn't working. Also, it causes Internet Explorer 6 SP2 to block the script...
12
by: Manfred Kooistra | last post by:
I have a problem with some JavaScript code not working. I'm sure I've done something obviously stupid, but I can't for the live of me figure it out. Can someone please help? This is the XHTML...
6
by: redridinghood | last post by:
Cant understand why my javascript is working in ie6 but not in mozilla. The javascript that i hav written for validationg my form is below - <script language="JavaScript"...
3
by: safiratec | last post by:
Hi, I want to show a div depending of the value of a <select> option, using 2 functions hidediv() and showdiv() - it is working fine in firefox, but not in IE (tested with IE 6 and 7). <body...
6
The1corrupted
by: The1corrupted | last post by:
I don't know if this is the right forum or not, but right now, my Javascript, that is embedded in PHP, is not working! It works everywhere else but not here. HELP!! <?php $soldnumber=$_POST;...
3
by: BillGatesFan | last post by:
Can someone please tell me why my javascript is producing an error on the page. It is not working on the following page http://www.rejoicecenter.org/calendar3.html Thank you
0
by: nitinsingh1 | last post by:
Hi, I am using a PDA containing Windows Mobile 5.0 for my client application in JAVA (JSP & SERVLET).There i am using a dropdown menu on a page.There are 3 different values (A,B,C) in that...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.