•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the JavaScript / DHTML / AJAX section within the Web Development category of DaniWeb, a massive community of 428,581 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 4,688 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our JavaScript / DHTML / AJAX advertiser: Lunarpages Web Hosting
Views: 1286 | Replies: 6
![]() |
•
•
Join Date: Apr 2006
Location: Sydney, Australia
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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:
<?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="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="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(age => 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 && <=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 + ";
document.write(content);
}
// stop hiding the script-->
</script>
</body>
</html>•
•
•
•
Originally Posted by
[code
[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:
"give only what u willing to receive "
•
•
Join Date: Apr 2006
Location: Sydney, Australia
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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:
What i have so far:
<?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>
<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!";
}
}
// stop hiding the script-->
</script>
</head>
<body>
comment= ("<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 (comment);
</body>
</html>•
•
Join Date: Jul 2006
Location: Deptford, London
Posts: 964
Reputation:
Rep Power: 5
Solved Threads: 48
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
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 4:13 am.
If it only works in Internet Explorer; it doesn't work.
•
•
Join Date: Apr 2006
Location: Sydney, Australia
Posts: 6
Reputation:
Rep Power: 0
Solved Threads: 0
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?
<?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()
{
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 = Math.round(w / (h*h));
switch(bmi)
{
case bmi <= 19:
message = "Underweight";
break;
case bmi >= 20 && bmi <= 25:
message = "Acceptable";
break;
case bmi >= 26 && bmi <=30:
message = "Overweight";
break;
case bmi >= 31:
message = "Obese";
break;
default: message = "Error!";
break;
}
comment = "<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 (comment);
}
// stop hiding the script-->
</script>
</head>
<body>
<a href="javascript: BMI_Calculator()">Calculate</a>
</body>
</html>•
•
Join Date: Mar 2007
Posts: 83
Reputation:
Rep Power: 0
Solved Threads: 2
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.
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.
![]() |
•
•
•
•
•
•
•
•
DaniWeb JavaScript / DHTML / AJAX Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
- Javascript links not working (Web Browsers)
- JavaScript's window.opener (JavaScript / DHTML / AJAX)
- recommendations for JavaScript learning resources? (JavaScript / DHTML / AJAX)
- Javascript/HTML problem!!! (JavaScript / DHTML / AJAX)
- Javascript Useful? (IT Careers and Business)
Other Threads in the JavaScript / DHTML / AJAX Forum
- Previous Thread: How to get the value of the checkbox which is generating dynamically
- Next Thread: how to get to next record on click of a button.



Linear Mode