Notices
Computer & Technology Related Post here for help and discussion of computing and related technology. Internet, TVs, phones, consoles, computers, tablets and any other gadgets.

JavaScript help please

Thread Tools
 
Search this Thread
 
Old 23 January 2009, 09:54 PM
  #1  
spufus
Scooby Regular
Thread Starter
iTrader: (2)
 
spufus's Avatar
 
Join Date: Oct 2003
Location: In the summerhouse
Posts: 661
Likes: 0
Received 0 Likes on 0 Posts
Default JavaScript help please

I've written this script to calculate the rough price to carpet a room.
Works fine in IE. It works if FF but instantly refreshes the form & clears the data.
Anyone any ideas?

Thanks in advance.

Code is below

<html>
<head>
<script type="text/javascript">

function roundTo4(raw)
{
strFloat = raw.toString();
var newFloat;

if(strFloat.indexOf(".") > 0 && strFloat.length - strFloat.indexOf(".") > 5)
{
strFloat = strFloat.substring(0,strFloat.indexOf(".") + 6);

var numFloat = parseFloat(strFloat);
var rndFloat = Math.round(numFloat * 10000);
var temp1Float = rndFloat / 10000;
var temp2Float = temp1Float.toString();

newFloat = temp2Float.substring(0,temp2Float.indexOf(".") + 5);
}
else
{
newFloat = strFloat;
}
return newFloat;
}

function roomCalc(form)
{
var p = form.p.value;
if(isNaN(p))
{
alert("Not a Number");
document.getElementById("p").value = "";
return false;
}

var w = form.w.value;
if(isNaN(w))
{
alert("Not a Number");
document.getElementById("w").value = "";
return false;
}

var l = form.l.value;
if(isNaN(l))
{
alert("Not a Number");
document.getElementById("l").value = "";
return false;
}

var volume = p * w * l;
var volumeOut = roundTo4(volume)
document.getElementById("bv").value = volumeOut;
}



</script>
</head>


<body>
<form>
<b>Room calculator</b><br />
<select id="p">
<option value="2">Karndean £2.00 sq metre</option>
<option value="2.5">Karndean £2.50 sq metre</option>
</select><br>

Room width (metres) approx: <input id="w" style="text-align:right"></input><br />
Room length (metres) approx: <input id="l" style="text-align:right"></input><br />
<button onclick="roomCalc(this.form)">Calculate</button><br>
Price: <input id="bv" style="text-align:right"></input>

</form>
</body>
</html>

Last edited by spufus; 23 January 2009 at 10:08 PM.
Old 23 January 2009, 10:13 PM
  #2  
Dedrater
Scooby Regular
 
Dedrater's Avatar
 
Join Date: May 2008
Posts: 3,957
Likes: 0
Received 0 Likes on 0 Posts
Default

It looks fine to me. Have you got the Runtime Environment plugin for FF? It will flash off if you havn't
Old 23 January 2009, 10:15 PM
  #3  
Dedrater
Scooby Regular
 
Dedrater's Avatar
 
Join Date: May 2008
Posts: 3,957
Likes: 0
Received 0 Likes on 0 Posts
Default

Could you post the calculation you are trying to achieve in base 10
Old 23 January 2009, 10:47 PM
  #4  
spufus
Scooby Regular
Thread Starter
iTrader: (2)
 
spufus's Avatar
 
Join Date: Oct 2003
Location: In the summerhouse
Posts: 661
Likes: 0
Received 0 Likes on 0 Posts
Default

Here it is with the rounding function gone:

Simpler, but still upsetting FF


<html>
<head>
<script type="text/javascript">

function roomCalc(form)
{
var p = form.p.value;
if(isNaN(p))
{
alert("Not a Number");
document.getElementById("p").value = "";
return false;
}

var w = form.w.value;
if(isNaN(w))
{
alert("Not a Number");
document.getElementById("w").value = "";
return false;
}

var l = form.l.value;
if(isNaN(l))
{
alert("Not a Number");
document.getElementById("l").value = "";
return false;
}

var volume = p * w * l;
document.getElementById("bv").value = volume;
}

</script>
</head>


<body>
<form>
<b>Room calculator</b><br />
<select id="p">
<option value="2.00">Karndean £2.00 sq metre</option>
<option value="2.50">Karndean £2.50 sq metre</option>
</select><br>

Room width (metres) approx: <input id="w" style="text-align:right"></input><br />
Room length (metres) approx: <input id="l" style="text-align:right"></input><br />
<button onclick="roomCalc(this.form)">Calculate</button><br>
Price: <input id="bv" style="text-align:right"></input>

</form>
</body>
</html>
Old 23 January 2009, 10:52 PM
  #5  
Dedrater
Scooby Regular
 
Dedrater's Avatar
 
Join Date: May 2008
Posts: 3,957
Likes: 0
Received 0 Likes on 0 Posts
Default

Makes no sense to me unless you post off the math mate

I need to know what you are trying to achieve, base 10 to me would mean the actual function.

Last edited by Dedrater; 23 January 2009 at 10:54 PM.
Old 23 January 2009, 10:55 PM
  #6  
Dedrater
Scooby Regular
 
Dedrater's Avatar
 
Join Date: May 2008
Posts: 3,957
Likes: 0
Received 0 Likes on 0 Posts
Default

Sorry, I don't know what you want to achieve?

Is that the whole program?

Last edited by Dedrater; 23 January 2009 at 10:57 PM.
Old 23 January 2009, 10:59 PM
  #7  
Dedrater
Scooby Regular
 
Dedrater's Avatar
 
Join Date: May 2008
Posts: 3,957
Likes: 0
Received 0 Likes on 0 Posts
Default

[QUOTE=Dedrater;8450451]
I need to know what you are trying to achieve,
Old 23 January 2009, 11:03 PM
  #8  
spufus
Scooby Regular
Thread Starter
iTrader: (2)
 
spufus's Avatar
 
Join Date: Oct 2003
Location: In the summerhouse
Posts: 661
Likes: 0
Received 0 Likes on 0 Posts
Default

Originally Posted by Dedrater
Sorry, I don't know what you want to achieve?

Is that the whole program?
Yes,

It calculates a rough cost to carpet a room. Carpet cost per metre in drop down * room width in text field * room length in text field.

The function works in IE & FF but in FF all fields are cleared for some reason.

Needs some more thought methinks....
Old 23 January 2009, 11:26 PM
  #9  
Dedrater
Scooby Regular
 
Dedrater's Avatar
 
Join Date: May 2008
Posts: 3,957
Likes: 0
Received 0 Likes on 0 Posts
Default

Karndean? is that a type of flooring/carpet?

anyway

Firefox is slimline on addons, what about Google's CHROME browser?
Old 23 January 2009, 11:32 PM
  #10  
Dedrater
Scooby Regular
 
Dedrater's Avatar
 
Join Date: May 2008
Posts: 3,957
Likes: 0
Received 0 Likes on 0 Posts
Default

I do understand btw


Last edited by Dedrater; 23 January 2009 at 11:36 PM.
Related Topics
Thread
Thread Starter
Forum
Replies
Last Post
StickyMicky
Computer & Technology Related
3
22 May 2004 03:02 PM
CodeKey@Lisan
Computer & Technology Related
11
20 February 2004 05:23 PM
RichB
Computer & Technology Related
2
05 November 2003 02:27 PM



Quick Reply: JavaScript help please



All times are GMT +1. The time now is 11:04 AM.