simple script inserted in websurvey software works in firefox but not in internet explorer

G

gray_slp

I am designing a web survey using surveymonkey.com and discovered I
could use javascript to modify their standard question formats much
the same as can be done in myspace.
I used this feature to insert a weight converter into my survey.
Unfortunately this script works fine in firefox v.2 but does not
cooperate with the surveymonkey software when used with internet
explorer v.6.
Can someone explain to me in simple terms why the script won't work
properly in both browsers? I paste the script below.

Many thanks,

Graham

<SCRIPT LANGUAGE="JavaScript">
<!-- all mult -->
function GetNumber1(form) {
var sNumber = parseFloat(stripBad(form.txtNumber.value));
var pw3 = parseFloat(form.txtNumbe2.value);
if (isNaN(sNumber)) sNumber = 0;
sNumber = sNumber*pw3
sNumber = gesult(sNumber)
form.txtResult.value= sNumber}

<!-- all divide -->
function GetNumber1b(form) {
var sNumber = parseFloat(stripBad(form.txtNumber.value));
var pw3 = parseFloat(form.txtNumbe2.value);
if (isNaN(sNumber)) sNumber = 0;
sNumber = sNumber/pw3
sNumber = gesult(sNumber)
form.txtResult.value= sNumber}

<!-- all mult -->
function GetNumber2(form) {
var sNumber = parseFloat(stripBad(form.txtNumber.value));
var pw3 = parseFloat(form.txtNumbe2.value)
var ml3 = parseFloat(form.txtNumbe3.value)
var cNum2 = 0
cNum2 = pw3*ml3
sNumber = sNumber*cNum2
sNumber = gesult(sNumber)
form.txtResult.value= sNumber}

<!-- all divide -->
function GetNumber2b(form) {
var sNumber = parseFloat(stripBad(form.txtNumber.value));
var pw3 = parseFloat(form.txtNumbe2.value)
var ml3 = parseFloat(form.txtNumbe3.value)
var cNum2 = 0
cNum2 = pw3*ml3
sNumber = sNumber/cNum2
sNumber = gesult(sNumber)
form.txtResult.value= sNumber}

function GetNumber22(form) {
var sAve = 0
var pw3 = parseFloat(stripBad(form.txtNumber.value));
var ml3 = parseFloat(stripBad(form.txtNumbe2.value));
if (isNaN(pw3)) pw3 = 0;
if (isNaN(ml3)) ml3 = 0;
sAve = (pw3*14) + ml3
sAve = gesult(sAve)
form.txtResult.value = sAve + " pounds" }

function GetNumber33(form) {
var sAve = 0
var pw3 = parseFloat(stripBad(form.txtNumber.value));
var md4 = 0
var lb4 = 0
md4 = pw3 % 14
lb4 = (pw3 - md4)/14
sAve = md4
sAve = gesult(sAve)
lb4 = comma(lb4)
form.txtResult.value = lb4 + " stone " + sAve + " pounds"
}


function GetNumber44(form) {
var sAve = 0
var pw3 = parseFloat(stripBad(form.txtNumber.value));
var ml3 = parseFloat(stripBad(form.txtNumbe2.value));
if (isNaN(pw3)) pw3 = 0;
if (isNaN(ml3)) ml3 = 0;
sAve = (pw3*14) + ml3
sAve = sAve * 0.45359237
sAve = gesult(sAve)
form.txtResult.value = sAve }
function stripBad(string) {
for (var i=0, output='', valid="eE-0123456789."; i<string.length; i
++)
if (valid.indexOf(string.charAt(i)) != -1)
output += string.charAt(i)
return output;
}

function gesult(ff){

if (Number.prototype.toFixed) {
ff = ff.toFixed(2);
ff = parseFloat(ff);
}
else {
var leftSide = Math.floor(ff);
var rightSide = ff - leftSide;
ff = leftSide + Math.round(rightSide *1e+14)/1e+14;
}

return comma(ff);
}

function comma(num) {
var n = Math.floor(num);
var myNum = num + "";
var myDec = ""

if (myNum.indexOf('.',0) > -1){
myDec = myNum.substring(myNum.indexOf('.',0),myNum.length);
}

var arr=new Array('0'), i=0;
while (n>0)
{arr=''+n%1000; n=Math.floor(n/1000); i++;}
arr=arr.reverse();
for (var i in arr) if (i>0)
while (arr.length<3) arr='0'+arr;
return arr.join() + myDec;
}

</SCRIPT>

</HEAD>
<a name = "sptokg"><H2> </H2></a>
<H5>*N.B. If you know your highest weight in stones and pounds, please
convert this to kg and enter the value in the box above</H5> <p>
<FORM>
Enter stones: <INPUT TYPE="text" NAME="txtNumber" SIZE=3> <B>and</B>
pounds: <INPUT TYPE="text" NAME="txtNumbe2" SIZE=3>
<INPUT TYPE="button" VALUE="Convert to Kg" class="b2t"
onclick="GetNumber44(this.form)"> <INPUT TYPE="text"
NAME="txtResult" SIZE=3> kg
<input type="reset" class="b3t">
</FORM>

<BR>
 
G

Geoffrey Summerhayes

I am designing a web survey using surveymonkey.com and discovered I
could use javascript to modify their standard question formats much
the same as can be done in myspace.
I used this feature to insert a weight converter into my survey.
Unfortunately this script works fine in firefox v.2 but does not
cooperate with the surveymonkey software when used with internet
explorer v.6.
Can someone explain to me in simple terms why the script won't work
properly in both browsers? I paste the script below.

Many thanks,

Graham

<SCRIPT LANGUAGE="JavaScript">
<!-- all mult -->
function GetNumber1(form) {
var sNumber = parseFloat(stripBad(form.txtNumber.value));
var pw3 = parseFloat(form.txtNumbe2.value);
if (isNaN(sNumber)) sNumber = 0;
sNumber = sNumber*pw3
sNumber = gesult(sNumber)
form.txtResult.value= sNumber}

<!-- all divide -->
function GetNumber1b(form) {
var sNumber = parseFloat(stripBad(form.txtNumber.value));
var pw3 = parseFloat(form.txtNumbe2.value);
if (isNaN(sNumber)) sNumber = 0;
sNumber = sNumber/pw3
sNumber = gesult(sNumber)
form.txtResult.value= sNumber}

<*snip*>

Because FF follows the ECMAScript specification
better.

But, just because you can, doesn't mean you
should, it just makes it more difficult to read.

<script type="text/javascript">

<!-- html comment hide script from older browsers

// all mult - javascript comment in code
// Of course as comments go, this one sucks
// because it doesn't really say anything
// useful
function GetNumber1(form)
{
var sNumber = parseFloat(stripBad(form.txtNumber.value));
var pw3 = parseFloat(form.txtNumbe2.value);

if (isNaN(sNumber)) sNumber = 0;
sNumber = sNumber*pw3; // semicolons!
sNumber = gesult(sNumber);
form.txtResult.value= sNumber;
}

etc.
 
G

Geoffrey Summerhayes

Geoffrey Summerhayes said the following on 6/26/2007 3:45 PM:


And what specification would that be?




Which are obscenely outdated, unneeded and can cause problems in certain
scenarios.

The reason that IE doesn't "see" the script is it sees the improper
comment as a conditional comment and ignores what is between them.



Which are totally irrelevant in the posted code and are 100% optional
unless you want to place all your statements on one line.

Fair enough, I got rid of the comments right off,
and the script still didn't work, but it worked
after placing the semi's. Did it three times too.
Must have affected something else without realising
it.

FWIW, I prefer explicit semicolons, I find it
easier to parse and I don't have to worry about
the exceptional cases where the automatic
punctuation doesn't insert them.
 
G

Geoffrey Summerhayes

Huh? Where the "automatic punctuation doesn't insert them"? Can you name
a parser that doesn't insert them? If you can, then it is ECMA broken as
semicolon insertion by the engine is specifically explained.

Not what I meant. Any time the result is ambiguous,
that is to say, both inserting a semicolon and
not inserting one result in syntactically correct
statements, the parser is obligated not to insert a
semicolon. If the end result of not inserting the
semicolon results in an error during execution, tough.

Yes, it's reasonably rare and a mistake on the coder's
part, but it's not that obvious and simply putting in
the semicolons makes one less thing to watch for.

Also, if you end up using any of the other C syntax
based languages where missing semicolons is an error,
you'll develop the habit anyway.

Finally, if you need to reduce the size of the javascript,
it's pretty much guaranteed using a compacter will
give better results.
 

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

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top