...

View Full Version : Prompt boxes with JavaScript



newbanks
01-23-2012, 06:29 PM
I am trying to get these prompt boxes to display for age and resting heart rate. I cannot get this to work...This is what I have...any suggestions?

<html>
<head>

<h1>Calculate Your Target Heart Rate</h1>

<p>You can calculate your heart rate so that you can get the maximun results from your cardiovascular workout.
Just follow these simple steps:</p>

<script type="text/javascript">
function show_prompt()
{
var number = prompt ("Please enter your Resting heart rate:","Enter Rest Heart Rate Here!");
if (number! null && number! = "")
var age = prompt ("Please enter your Age", "Enter Your Age Here!");

{
document.write ("Your Resting Heart Rate is" + number);
document.write ("Your Age is" + Age);
}
}

</script>
</head>
<body>

<input type ="button" onclick = "show_prompt ()" value = "Start Calculating Here!"/>


</body>
</html>

Logic Ali
01-23-2012, 06:47 PM
any suggestions?Yes - use the error console.

Philip M
01-23-2012, 07:53 PM
Have a look at the position ofyour braces { and }

When posting here please help us to help you by following the posting guidelines and wrapping your code in CODE tags. This means use the octothorpe or # button on the toolbar. You can (and should) edit your previous post.



<html>
<head>

<h1>Calculate Your Target Heart Rate</h1>

<p>You can calculate your heart rate so that you can get the maximun results from your cardiovascular workout.
Just follow these simple steps:</p>

<script type="text/javascript">
function show_prompt() {
for (var i=1; i<2; i++) {
var h = parseInt(prompt ("Please enter your Resting heart rate:","Enter Rest Heart Rate Here!"));
if ((isNaN(h)) || (h == "")) {
alert ("You must enter a number!! ");
i -- ;
}
}

for (var i=1; i<2; i++) {
var age = parseInt(prompt ("Please enter your Age", "Enter Your Age Here!"));
if ((isNaN(age)) || (age == "")) {
alert ("You must enter a number!! ");
i -- ;
}
}

document.write ("Your Resting Heart Rate is " + h + "<br>");
document.write ("Your Age is " + age);
}

</script>
</head>
<body>

<input type ="button" onclick = "show_prompt ()" value = "Start Calculating Here!"/>


</body>
</html>

Note that document.write() is long obsolete. Also that Javascript is case-sensitive and Age != age.

It is not very sensible to name a Javascript variable "number".

Although the user must enter numbers, there is no check that the numbers are sensible.

"Life is like a mirror, we get the best results when we smile at it. " - Anonymous

felgall
01-23-2012, 09:36 PM
Using prompt() to collect data is also not advisable as some browsers will display an extra option to either turn off further dialogs or to turn off Javascript completely as the dialogs built into JavaScript are now intended only to be used for debugging purposes.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum