works in html form but not when in external JS file

F

farouqdin

Hi everyone,

I've just developed little code which calculates a certain score
depending on there values. The javascript works in the form. However
when i tried putting on to an external javascript document
("parScore.js"). It doesn't calculate the way it suppose to. Its like
it needs the reference from the form fields :s

I've referenced it as this in the html....

<script src="javascript/parScore.js"></script>


My javascript file contains this code....

<!--

// variables to hold the values of basic obs
var vHR = document.getElementById('HR').value;
var vTemp = document.getElementById('Temp').value;
var vRespRate = document.getElementById('RespRate').value;
var vBP = document.getElementById('BP').value;
var vSa02 = document.getElementById('Sa02').value;
var vGCS = document.getElementById('GCS').value;
var vUrineOutput = document.getElementById('UrineOutput').value;
var vLevelOfCons = document.getElementById('LevelOfCons').value;
var vO2Stat = document.getElementById('O2Stat').value;

//variables to hold the score
var vScoreHR;
var vScoreTemp;
var vScoreRespRate;
var vScoreBP;
var vScoreSa02;
var vScoreGCS;
var vScoreUrineOutput;
var vScoreLevelOfCons;
var vScoreO2Stat;


// PARSCORE
var vParScore

function CalcuateParScore() {




//----------HR CALC
if (vHR >= 51 && vHR <= 100)
{
vScoreHR = 0;
}
else if (vHR >= 41 && vHR <= 50)
{
vScoreHR = 1;
}
else if (vHR <= 40)
{
vScoreHR = 2;
}
else if (vHR >= 101 && vHR <= 110)
{
vScoreHR = 1;
}
else if (vHR >= 111 && vHR <= 129)
{
vScoreHR = 2;
}
else if (vHR >= 130)
{
vScoreHR = 3;
}
else
{
vScoreHR = 0;
}


//----------HR CALC
if (vTemp >= 36 && vTemp <= 37.5)
{
vScoreTemp = 0;
}
else if (vTemp >= 37.6 && vTemp <= 38.4)
{
vScoreTemp = 1;
}
else if (vTemp >= 38.5)
{
vScoreTemp = 2;
}
else if (vTemp >= 35.1 && vTemp <= 35.9)
{
vScoreTemp = 1;
}
else if (vTemp <= 35)
{
vScoreTemp = 2;
}
else
{
vScoreTemp = 0;
}



//--------RESPIRATORY RATE
if (vRespRate <= 8)
{
vScoreRespRate = 2;
}
else if (vRespRate >= 9 && vRespRate <= 24)
{
vScoreRespRate = 0;
}
else if (vRespRate >= 25 && vRespRate <= 29)
{
vScoreRespRate = 2;
}
else if (vRespRate >= 30)
{
vScoreRespRate = 3;
}
else
{
vScoreRespRate = 0;
}

//--------BP
if (vBP >= 101 && vBP <= 199)
{
vScoreBP = 0;
}
else if (vBP >= 200)
{
vScoreBP = 2;
}
else if (vBP >= 81 && vBP <= 100)
{
vScoreBP = 1;
}
else if (vBP >= 71 && vBP <= 80)
{
vScoreBP = 2;
}
else if (vBP <= 70)
{
vScoreBP = 3;
}
else
{
vScoreBP = 0;
}

//--------OXYGEN SATS
if (vSa02 >= 91 && vSa02 <= 94)
{
vScoreSa02 = 1;
}
else if (vSa02 >= 95)
{
vScoreSa02 = 0;
}
else if (vSa02 >= 89 && vSa02 <= 90)
{
vScoreSa02 = 2;
}
else if (vSa02 <= 88)
{
vScoreSa02 = 3;
}
else
{
vScoreSa02 = 0;
}

//--------OXYGEN SATS
if (vO2Stat == ">=8 Lits/40%")
{
vScoreO2Stat = 1;

}
else
{
vScoreO2Stat = 0;
}


//--------Urine Output
if (vUrineOutput == "<=20ml/hr for 2hrs")
{
vScoreUrineOutput = 3;
}
else if (vUrineOutput == "<=1ml/kg over for 2hrs")
{
vScoreUrineOutput = 2;
}
else if (vUrineOutput == ">=500ml in 24hrs")
{
vScoreUrineOutput = 0;
}
else if (vUrineOutput == "250-500mls in 24 hrs")
{
vScoreUrineOutput = 1;
}
else if (vUrineOutput == "<=250mls in 24 hrs")
{
vScoreUrineOutput = 2;
}
else if (vUrineOutput == "Nil")
{
vScoreUrineOutput = 3;
}

else
{
vScoreUrineOutput = 0;
}



//--------LEVEL OF CONCSCIOUSNESS
if (vLevelOfCons == "Alert")
{
vScoreLevelOfCons = 0;
}
else if (vLevelOfCons == "Drowsy Responds to voice")
{
vScoreLevelOfCons = 1;
}
else if (vLevelOfCons == "Acute confusion or agitation")
{
vScoreLevelOfCons = 2;
}
else if (vLevelOfCons == "Responds to pain only")
{
vScoreLevelOfCons = 3;
}
else
{
vScoreLevelOfCons = 0;
}


// Adding the parscore for each vital ob
vParScore = vScoreHR + vScoreTemp + vScoreRespRate + vScoreBP +
vScoreSa02 + vScoreO2Stat + vScoreUrineOutput + vScoreLevelOfCons

//Displaying the result into the parscore field
alert(vParScore);
document.getElementById('Parscore').value = vParScore;

//document.myform.ParScore.value=vHR

}

-->

The code does work in the html file. But to an external .js file it
does put something in the parscore field and msgbox does appear. But i
get the value 0 all the time. Regardless of me filling in the fields
in the html form.

Have i missed something out?

cheers

Farouq
 
S

SAM

(e-mail address removed) a écrit :
Hi everyone,

I've just developed little code which calculates a certain score
depending on there values. The javascript works in the form. However
when i tried putting on to an external javascript document
("parScore.js"). It doesn't calculate the way it suppose to. Its like
it needs the reference from the form fields :s

I've referenced it as this in the html....

<script src="javascript/parScore.js"></script>

<script src="javascript/parScore.js" type="text/javascript"></script>
</head>
<body>
<form onsubmit="CalcuateParScore();return false" blahh >
blah
...
<input type=submit>
</form>


the file 'parScore.js' has no tag 'script'
 
D

david.karr

Hi everyone,

I've just developed little code which calculates a certain score
depending on there values. The javascript works in the form. However
when i tried putting on to an external javascript document
("parScore.js"). It doesn't calculate the way it suppose to. Its like
it needs the reference from the form fields :s

I've referenced it as this in the html....

<script src="javascript/parScore.js"></script>

My javascript file contains this code....

<!--

// variables to hold the values of basic obs
var vHR = document.getElementById('HR').value;
var vTemp = document.getElementById('Temp').value;
var vRespRate = document.getElementById('RespRate').value;
var vBP = document.getElementById('BP').value;
var vSa02 = document.getElementById('Sa02').value;
var vGCS = document.getElementById('GCS').value;
var vUrineOutput = document.getElementById('UrineOutput').value;
var vLevelOfCons = document.getElementById('LevelOfCons').value;
var vO2Stat = document.getElementById('O2Stat').value;

//variables to hold the score
var vScoreHR;
var vScoreTemp;
var vScoreRespRate;
var vScoreBP;
var vScoreSa02;
var vScoreGCS;
var vScoreUrineOutput;
var vScoreLevelOfCons;
var vScoreO2Stat;

// PARSCORE
var vParScore

function CalcuateParScore() {

//----------HR CALC
if (vHR >= 51 && vHR <= 100)
{
vScoreHR = 0;
}
else if (vHR >= 41 && vHR <= 50)
{
vScoreHR = 1;
}
else if (vHR <= 40)
{
vScoreHR = 2;
}
else if (vHR >= 101 && vHR <= 110)
{
vScoreHR = 1;
}
else if (vHR >= 111 && vHR <= 129)
{
vScoreHR = 2;
}
else if (vHR >= 130)
{
vScoreHR = 3;
}
else
{
vScoreHR = 0;
}

//----------HR CALC
if (vTemp >= 36 && vTemp <= 37.5)
{
vScoreTemp = 0;
}
else if (vTemp >= 37.6 && vTemp <= 38.4)
{
vScoreTemp = 1;
}
else if (vTemp >= 38.5)
{
vScoreTemp = 2;
}
else if (vTemp >= 35.1 && vTemp <= 35.9)
{
vScoreTemp = 1;
}
else if (vTemp <= 35)
{
vScoreTemp = 2;
}
else
{
vScoreTemp = 0;
}

//--------RESPIRATORY RATE
if (vRespRate <= 8)
{
vScoreRespRate = 2;
}
else if (vRespRate >= 9 && vRespRate <= 24)
{
vScoreRespRate = 0;
}
else if (vRespRate >= 25 && vRespRate <= 29)
{
vScoreRespRate = 2;
}
else if (vRespRate >= 30)
{
vScoreRespRate = 3;
}
else
{
vScoreRespRate = 0;
}

//--------BP
if (vBP >= 101 && vBP <= 199)
{
vScoreBP = 0;
}
else if (vBP >= 200)
{
vScoreBP = 2;
}
else if (vBP >= 81 && vBP <= 100)
{
vScoreBP = 1;
}
else if (vBP >= 71 && vBP <= 80)
{
vScoreBP = 2;
}
else if (vBP <= 70)
{
vScoreBP = 3;
}
else
{
vScoreBP = 0;
}

//--------OXYGEN SATS
if (vSa02 >= 91 && vSa02 <= 94)
{
vScoreSa02 = 1;
}
else if (vSa02 >= 95)
{
vScoreSa02 = 0;
}
else if (vSa02 >= 89 && vSa02 <= 90)
{
vScoreSa02 = 2;
}
else if (vSa02 <= 88)
{
vScoreSa02 = 3;
}
else
{
vScoreSa02 = 0;
}

//--------OXYGEN SATS
if (vO2Stat == ">=8 Lits/40%")
{
vScoreO2Stat = 1;

}
else
{
vScoreO2Stat = 0;
}

//--------Urine Output
if (vUrineOutput == "<=20ml/hr for 2hrs")
{
vScoreUrineOutput = 3;
}
else if (vUrineOutput == "<=1ml/kg over for 2hrs")
{
vScoreUrineOutput = 2;
}
else if (vUrineOutput == ">=500ml in 24hrs")
{
vScoreUrineOutput = 0;
}
else if (vUrineOutput == "250-500mls in 24 hrs")
{
vScoreUrineOutput = 1;
}
else if (vUrineOutput == "<=250mls in 24 hrs")
{
vScoreUrineOutput = 2;
}
else if (vUrineOutput == "Nil")
{
vScoreUrineOutput = 3;
}

else
{
vScoreUrineOutput = 0;
}

//--------LEVEL OF CONCSCIOUSNESS
if (vLevelOfCons == "Alert")
{
vScoreLevelOfCons = 0;
}
else if (vLevelOfCons == "Drowsy Responds to voice")
{
vScoreLevelOfCons = 1;
}
else if (vLevelOfCons == "Acute confusion or agitation")
{
vScoreLevelOfCons = 2;
}
else if (vLevelOfCons == "Responds to pain only")
{
vScoreLevelOfCons = 3;
}
else
{
vScoreLevelOfCons = 0;
}

// Adding the parscore for each vital ob
vParScore = vScoreHR + vScoreTemp + vScoreRespRate + vScoreBP +
vScoreSa02 + vScoreO2Stat + vScoreUrineOutput + vScoreLevelOfCons

//Displaying the result into the parscore field
alert(vParScore);
document.getElementById('Parscore').value = vParScore;

//document.myform.ParScore.value=vHR

}

-->

The code does work in the html file. But to an external .js file it
does put something in the parscore field and msgbox does appear. But i
get the value 0 all the time. Regardless of me filling in the fields
in the html form.

Have i missed something out?

Do you really have comment markers at the beginning and end of the js
file? If so, remove them. You don't need them when it's in the HTML,
either. That was used for supporting obsolete browsers.

If that still doesn't work, try installing Firebug (you should anyway)
and debugging the code in Firefox.
 
S

SAM

(e-mail address removed) a écrit :
The code does work in the html file.

In your html file this code as given can only works if it is in end of
the file

after the navigator have seen the elements referenced in the script :

// variables to hold the values of basic obs
var vHR = document.getElementById('HR').value;
var vTemp = document.getElementById('Temp').value;
var vRespRate = document.getElementById('RespRate').value;
var vBP = document.getElementById('BP').value;
var vSa02 = document.getElementById('Sa02').value;
var vGCS = document.getElementById('GCS').value;
var vUrineOutput = document.getElementById('UrineOutput').value;
var vLevelOfCons = document.getElementById('LevelOfCons').value;
var vO2Stat = document.getElementById('O2Stat').value;



But to an external .js file it
does put something in the parscore field and msgbox does appear. But i
get the value 0 all the time. Regardless of me filling in the fields
in the html form.

Have i missed something out?

put your global variables values somewhere the navigator can understand
of what you talk (after loading ?)


// begin script :

// variables to hold the values of basic obs
var vHR, vTemp, vRespRate, vBP, vSa02,
vGCS, vUrineOutput, vLevelOfCons, vO2Stat;

window.onload = function() {
vHR = document.getElementById('HR').value;
vTemp = document.getElementById('Temp').value;
vRespRate = document.getElementById('RespRate').value;
vBP = document.getElementById('BP').value;
vSa02 = document.getElementById('Sa02').value;
vGCS = document.getElementById('GCS').value;
vUrineOutput = document.getElementById('UrineOutput').value;
vLevelOfCons = document.getElementById('LevelOfCons').value;
vO2Stat = document.getElementById('O2Stat').value;
}

//variables to hold the score
var vScoreHR,
vScoreTemp,
vScoreRespRate,
vScoreBP,
vScoreSa02,
vScoreGCS,
vScoreUrineOutput,
vScoreLevelOfCons,
vScoreO2Stat;

// PARSCORE
var vParScore;

function CalcuateParScore() {

blah
blah

}

// end of script
 
S

SAM

(e-mail address removed) a écrit :
// Adding the parscore for each vital ob
vParScore = vScoreHR + vScoreTemp + vScoreRespRate + vScoreBP +
vScoreSa02 + vScoreO2Stat + vScoreUrineOutput + vScoreLevelOfCons

that can't work except if each of them is a number

The function could have to be very simple ...



<html>
<title>Basic Observations</title>
<script type="text/javascript">
function CalculateParScore() {
var f = document.BasicObs, t = 0;
for(var i=1; i<f.length-3; i++) t += f.value*1;
f.Parscore.value = t;
return false;
}
window.onload = function() {
var f = document.BasicObs;
for(var i=1; i<f.length-3; i++)
f.onchange = CalculateParScore;
}
</script>
<style type="text/css">
html, body { background: white; font-family: geneva, verdana, arial;}
form { text-align:center}
select { width: 200px; text-align: center; }
fieldset { width: 50%; border:1px solid;padding:9px; text-align: right;
background: #D1E0EF; margin: auto; }
legend { border:1px solid; background: #ff9; padding: 3px 9px;
margin: 6px }
</style>
</head>
<body>
<form name="BasicObs" action="#"
onsubmit="return CalculateParScore();">
<fieldset><legend> BASIC OBSERVATIONS </legend>

<p>HR CALC
<select name="vScoreHR">
<option value="2"> vHR <= 40 </option>
<option value="1"> 41 <= vHR <= 50 </option>
<option value="0" selected> 51 <= vHR <= 100 </option>
<option value="1"> 101 <= vHR <= 110 </option>
<option value="2"> 111 <= vHR <= 129 </option>
<option value="3"> vHR >= 130 </option>
</select>


<p>HR CALC (vTemp) :
<select name="vScoreTemp">
<option value="2"> 35 and less </option>
<option value="1"> from 35.1 to 35.9 </option>
<option value="0" selected> from 36 to 37.5 </option>
<option value="1"> from 37.6 to 38.4 </option>
<option value="2"> 38.5 and more </option>
</select>



<p>RESPIRATORY RATE :
<select name="vScoreRespRate">
<option value="2"> 8 and less </option>
<option value="0" selected> from 9 to 24 </option>
<option value="2"> from 25 to 29 </option>
<option value="3"> 30 and more </option>
</select>

<p>BP :
<select name="vScoreBP">
<option value="2"> vBP >= 200 </option>
<option value="0" selected> 101 <= vBP <= 199 </option>
<option value="1"> 81 <= vBP <= 100 </option>
<option value="2"> 71 <= vBP <= 80 </option>
<option value="3"> vBP <= 70 </option>
</select>

<p>OXYGEN SATS (vScoreSa02) :
<select name="vScoreSa02">
<option value="0" selected> vSa02 >= 95 </option>
<option value="1"> 91 <= vSa02 <= 94 </option>
<option value="2"> 89 <= vSa02 <= 90 </option>
<option value="3"> vSa02 <= 88 </option>
</select>

<p>OXYGEN SATS (vScoreO2Stat) :
<select name="vScoreO2Stat">
<option value="0" selected> unknown </option>
<option value="1"> >=8 Lits/40%" </option>
</select>

<p>Urine Output :
<select name="vScoreUrineOutput">
<option value="3"> <=20ml/hr for 2hrs </option>
<option value="2"> <=1ml/kg over for 2hrs </option>
<option value="0" selected> >=500ml in 24hrs </option>
<option value="1"> 250-500mls in 24 hrs </option>
<option value="2"> <=250mls in 24 hrs </option>
<option value="3"> Nil </option>
</select>

<p>LEVEL OF CONCSCIOUSNESS :
<select name="vScoreLevelOfCons">
<option value="0" selected> Alert </option>
<option value="1"> Drowsy Responds to voice </option>
<option value="2"> Acute confusion or agitation</option>
<option value="3"> Responds to pain only </option>
</select>

<p><input type=submit value="RESULT :">
<input name="Parscore" style="width:20px;color:red;margin:auto
20px;text-align:center">
<input type=reset value=reset></p>
</fieldset>
</form>
</body>
</html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,534
Members
45,008
Latest member
Rahul737

Latest Threads

Top