944,156 Members | Top Members by Rank

Ad:
Mar 20th, 2007
0

Javascript help!

Expand Post »
Hi everyone, I'm pretty new to Javascript and I guess I'm pretty nooby at it so I have a problem with some Javascript I'm trying to write. Basically I'm creating some Javascript that will allow me to calculate someone's Body Mass Index (BMI) and so far with my code have absolutely no idea what's wrong with it. If you guys could lead me in the right direction it would be much appreciated :cheesy:

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6.  
  7. <head><title>BMI Calculator</title>
  8.  
  9. <script type="javascript">
  10. <!-- Hide script from non-script-friendly browsers
  11. function BMI Calculator(bmi)
  12. {
  13. var bmi = w / (h*h);
  14. return bmi;
  15. }
  16. //stop hiding the script-->
  17. </script>
  18. </head>
  19.  
  20. <body>
  21. <script type="javascript">
  22. <!-- Hide script from non-script-friendly browsers
  23. var n;
  24. var a;
  25. var h;
  26. var w;
  27. var bmi;
  28. n = prompt("What is your age? ","");
  29. if (n <= 17)
  30. {
  31. document.write("We are unable to calculate your BMI");
  32. }
  33. else if(age => 18)
  34. {
  35. h = prompt("What is your height in metres?","");
  36. w = prompt("What is your weight in kilograms?","");
  37. bmi = w / (h*h);
  38.  
  39. if (bmi <= 19)
  40. {
  41. message = "Underweight";
  42. }
  43. else if (bmi >= 20 && bmi <= 25)
  44. {
  45. message = "Acceptable";
  46. }
  47. else if (bmi >= 26 && <=30)
  48. {
  49. message = "Overweight";
  50. }
  51. else if (bmi >= 31)
  52. {
  53. message = "Obese";
  54. }
  55. else
  56. {
  57. message = "Error!";
  58. }
  59.  
  60. content = ("<html><head><title>Your BMI</title></head><body><h1>BODY MASS INDEX (BMI) CALCULATOR</h1>
  61. <p>Your height= " + h + "<br>Your weight= " + w + <br>Your BMI= " + bmi + "</p>
  62. <p><h2>what your BMI Indicates</h2><br>" + message + ";
  63.  
  64. document.write(content);
  65. }
  66. // stop hiding the script-->
  67. </script>
  68. </body>
  69. </html>
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viet_mafia is offline Offline
6 posts
since Apr 2006
Mar 20th, 2007
0

Re: Javascript help!

Quote originally posted by
[code ...
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">

<head><title>BMI Calculator</title>

<script type="text/javascript">
<!-- Hide script from non-script-friendly browsers
function BMI Calculator(bmi)
{
var bmi = w / (h*h);
return bmi;
}
//stop hiding the script-->
</script>
</head>

<body>
<script type="text/javascript">
<!-- Hide script from non-script-friendly browsers
var n;
var a;
var h;
var w;
var bmi;
n = prompt("What is your age? ","");
if (n <= 17)
{
document.write("We are unable to calculate your BMI");
}
else if(n => 18)
{
h = prompt("What is your height in metres?","");
w = prompt("What is your weight in kilograms?","");
bmi = w / (h*h);

if (bmi <= 19)
{
message = "Underweight";
}
else if (bmi >= 20 && bmi <= 25)
{
message = "Acceptable";
}
else if (bmi >= 26 && bmi <=30)
{
message = "Overweight";
}
else if (bmi >= 31)
{
message = "Obese";
}
else
{
message = "Error!";
}

content = ("<html><head><title>Your BMI</title></head><body><h1>BODY MASS INDEX (BMI) CALCULATOR</h1>
<p>Your height= " + h + "<br>Your weight= " + w + "<br>Your BMI= " + bmi + "</p>
<p><h2>what your BMI Indicates</h2><br>" + message + "</p>");

document.write(content);
}
// stop hiding the script-->
</script>
</body>
</html>[/code]
it had simple mistakes , after i correct it , it works with me and my bmi was acceptable :cheesy:
Reputation Points: 37
Solved Threads: 17
Junior Poster
manal is offline Offline
122 posts
since Mar 2006
Mar 20th, 2007
0

Re: Javascript help!

Your scripts go in the head, not the body.
Reputation Points: 730
Solved Threads: 181
Nearly a Senior Poster
MidiMagic is offline Offline
3,314 posts
since Jan 2007
Mar 21st, 2007
0

Re: Javascript help!

Hey guys, i followed your suggestions but it still doesnt work for me. I don't get any prompts or anything. I've done the right thing and saved it as javascript.js haven't I? or should i save it as something else? thanks for the help so far.

What i have so far:
JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6.  
  7. <head><title>BMI Calculator</title>
  8.  
  9. <script type="text/javascript">
  10. <!-- Hide script from non-script-friendly browsers
  11. function BMI Calculator(bmi)
  12. {
  13. var bmi = w / (h*h);
  14. return bmi;
  15. }
  16. //stop hiding the script-->
  17. </script>
  18.  
  19. <script type="text/javascript">
  20. <!-- Hide script from non-script-friendly browsers
  21. var n;
  22. var a;
  23. var h;
  24. var w;
  25. var bmi;
  26. n = prompt("What is your age? ","");
  27. if (n <= 17)
  28. {
  29. document.write("We are unable to calculate your BMI");
  30. }
  31. else if(n => 18)
  32. {
  33. h = prompt("What is your height in metres?","");
  34. w = prompt("What is your weight in kilograms?","");
  35. bmi = w / (h*h);
  36.  
  37. if (bmi <= 19)
  38. {
  39. message = "Underweight";
  40. }
  41. else if (bmi >= 20 && bmi <= 25)
  42. {
  43. message = "Acceptable";
  44. }
  45. else if (bmi >= 26 && bmi <=30)
  46. {
  47. message = "Overweight";
  48. }
  49. else if (bmi >= 31)
  50. {
  51. message = "Obese";
  52. }
  53. else
  54. {
  55. message = "Error!";
  56. }
  57. }
  58. // stop hiding the script-->
  59. </script>
  60. </head>
  61.  
  62. <body>
  63.  
  64. comment= ("<html><head><title>Your BMI</title></head><body><h1>BODY MASS INDEX (BMI) CALCULATOR</h1>
  65. <p>Your height= " + h + "<br>Your weight= " + w + "<br>Your BMI= " + bmi + "</p>
  66. <p><h2>what your BMI Indicates</h2><br>" + message + "</p>");
  67.  
  68. document.write (comment);
  69. </body>
  70. </html>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viet_mafia is offline Offline
6 posts
since Apr 2006
Mar 21st, 2007
0

Re: Javascript help!

it's permissable to write <script> in the body section; be aware that the script will run at weird times though ( as the script element is parsed ). the only way the script you have got WILL work is if part of that script runs as the body is parsed; because you use document.write( ); which is parse/position dependant ( i.e. you NEED the script to run as the script element within the body is parsed )

be aware; document.write( ) doesn't work in browsers like Firefox - when they goes into 'true' XML parsing mode. That XHTML DOCTYPE might send Firefox into XML mode... in which case; Firefox will do unpredictable things with document.write ( i.e. nothing ):

http://www.mozilla.org/docs/web-deve...html#xhtmldiff

I would say you'll be alright; since I doubt you're sending your XHTML page with the application/xhtml+xml content type header - but regardless! document.write( ) isn't 'permitted' in XHTML:

http://www.w3.org/MarkUp/2004/xhtml-faq#docwrite

try the code that manal posted; but stick a nice HTML4.0 transitional Doctype on top; instead of that XHTML1.0 strict one
Last edited by MattEvans; Mar 21st, 2007 at 5:13 am.
Moderator
Featured Poster
Reputation Points: 522
Solved Threads: 64
Veteran Poster
MattEvans is offline Offline
1,091 posts
since Jul 2006
Mar 21st, 2007
0

Re: Javascript help!

Thanks for the tips Matt, however I'm restricted to using only XHTML so I don't really have a choice. I wrote up another script but when i click on calculate it does nothing and just shows theres an error on the page. Could anyone help please?

JavaScript / DHTML / AJAX Syntax (Toggle Plain Text)
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
  3. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
  4.  
  5. <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
  6.  
  7. <head><title>BMI Calculator</title>
  8.  
  9. <script type="text/javascript">
  10. <!-- Hide script from non-script-friendly browsers
  11. function BMI_Calculator()
  12. {
  13. n = prompt('What is your age?',"");
  14.  
  15. if (n <= 17)
  16. { document.write("We are unable to calculate your BMI"); }
  17. else if(n => 18)
  18. { h = prompt('What is your height in metres?',"");
  19. w = prompt('What is your weight in kilograms?',"");
  20. bmi = Math.round(w / (h*h));
  21.  
  22. switch(bmi)
  23. {
  24. case bmi <= 19:
  25. message = "Underweight";
  26. break;
  27. case bmi >= 20 && bmi <= 25:
  28. message = "Acceptable";
  29. break;
  30. case bmi >= 26 && bmi <=30:
  31. message = "Overweight";
  32. break;
  33. case bmi >= 31:
  34. message = "Obese";
  35. break;
  36. default: message = "Error!";
  37. break;
  38. }
  39.  
  40. comment = "<html><head><title>Your BMI</title></head><body>" +
  41. "<h1>BODY MASS INDEX (BMI) CALCULATOR</h1><p>Your height = " +
  42. h + "<br>Your weight = " + w + "<br>Your BMI = " + bmi + "</p><p>" +
  43. "<h2>what your BMI Indicates</h2><br>" + message + "</p>";
  44.  
  45. document.write (comment);
  46.  
  47. }
  48. // stop hiding the script-->
  49. </script>
  50. </head>
  51.  
  52. <body>
  53.  
  54. <a href="javascript: BMI_Calculator()">Calculate</a>
  55.  
  56. </body>
  57. </html>
Reputation Points: 10
Solved Threads: 0
Newbie Poster
viet_mafia is offline Offline
6 posts
since Apr 2006
Mar 23rd, 2007
0

Re: Javascript help!

You have 2 scripts, 1 in the head and 1 in the body, but the 1 in the body is writing a new html, head, body etc. tags... so you have a broken html page for the browser when you are done....

In effect you have this structure
html
head
script
/script
/head
body
script
html
head
body
/script
/body
/html

If that doesn't make people go "hun?" as well as your browser, especially when it is set to "strict" I don't know what will....

Computers are the smartest thing in the world... they do only and exactly what we tell them, nothing more, nothing less. They show us our own mistakes in bright vibrant colors and sometimes publish them for the world to see. ALL errors are user errors, computers don't make mistakes, people do.
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
rgtaylor is offline Offline
83 posts
since Mar 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JavaScript / DHTML / AJAX Forum Timeline: How to get the value of the checkbox which is generating dynamically
Next Thread in JavaScript / DHTML / AJAX Forum Timeline: using javascript to communicate with flash





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC