Page 1 of 1

[SOLVED] Javascript not working with Safari

Posted: Thu Jul 28, 2005 10:54 am
by hairyjim
Hi,

I have the following code which works in FireFox and IE but for some reason and I have no idea why it does nto work in Safari.

Could someone please tell me why, it is driving me nuts!

Code: Select all

<!DOCTYPE HTML PUBLIC &quote;-//W3C//DTD HTML 4.01 Transitional//EN&quote; &quote;http://www.w3.org/TR/html4/loose.dtd&quote;>
<html>
<head>
<meta http-equiv=&quote;Content-Type&quote; content=&quote;text/html; charset=iso-8859-1&quote;>
<title>Calculator</title>
<script type=&quote;text/javascript&quote;>

function smatotal()
{

var smaqty = 0;

	if (document.Calc.restqty.value >= 1)
	{
		smaqty++;
	}		

	if (document.Calc.visqty.value >= 1)
	{
		smaqty++;
	}		

	if (document.Calc.classqty.value >= 1)
	{
		smaqty++;
	}		
	
	if (document.Calc.acqqty.value >= 1)
	{
		smaqty++;
	}		

return smaqty;
}

function addsmatotal()
{

var addsmaqty = 0;

	if (document.Calc.restqty.value > 1)
	{
		addsmaqty = addsmaqty + (document.Calc.restqty.value - 1);
	}		

	if (document.Calc.visqty.value > 1)
	{
		addsmaqty = addsmaqty + (document.Calc.visqty.value - 1);
	}		

	if (document.Calc.classqty.value > 1)
	{
		addsmaqty = addsmaqty + (document.Calc.classqty.value - 1);
	}		
	
	if (document.Calc.acqqty.value > 1)
	{
		addsmaqty = addsmaqty + (document.Calc.acqqty.value - 1);
	}		

return addsmaqty;
}
</script>
<link href=&quote;/css/verdana_table.css&quote; rel=&quote;stylesheet&quote; type=&quote;text/css&quote;>
</head>
<body class=&quote;BodyText&quote;>
<CENTER>
<FORM NAME=&quote;Calc&quote;>
<table width=&quote;250&quote; border=&quote;0&quote; cellpadding=&quote;0&quote; cellspacing=&quote;5&quote; class=&quote;TableBorder&quote;>
  <tr class=&quote;Title&quote;>
    <td width=&quote;200&quote;>Product</td>
    <td width=&quote;50&quote;>Qty</td>
  </tr>
  <tr>
    <td width=&quote;200&quote;>Restoration</td>
    <td width=&quote;50&quote;><input name=&quote;restqty&quote; type=&quote;text&quote; id=&quote;restqty&quote; value=&quote;0&quote; size=&quote;2&quote; maxlength=&quote;2&quote;></td>
  </tr>
  <tr>
    <td width=&quote;200&quote;>Visualization</td>
    <td width=&quote;50&quote;><input name=&quote;visqty&quote; type=&quote;text&quote; id=&quote;visqty&quote; value=&quote;0&quote; size=&quote;2&quote; maxlength=&quote;2&quote;></td>
  </tr>
  <tr>
    <td width=&quote;200&quote;>Classification</td>
    <td width=&quote;50&quote;><input name=&quote;classqty&quote; type=&quote;text&quote; id=&quote;classqty&quote; value=&quote;0&quote; size=&quote;2&quote; maxlength=&quote;2&quote;></td>
  </tr>
  <tr>
    <td width=&quote;200&quote;>Acquisition</td>
    <td width=&quote;50&quote;><input name=&quote;acqqty&quote; type=&quote;text&quote; id=&quote;acqqty&quote; value=&quote;0&quote; size=&quote;2&quote; maxlength=&quote;2&quote;></td>
  </tr>
  <tr>
    <td colspan=&quote;2&quote;>
      <div align=&quote;center&quote;>
        <input name=&quote;Calculate&quote; type=&quote;button&quote; value=&quote;Calculate&quote; onclick=&quote;window.location.reload()&quote;>
        <input type=&quote;reset&quote; name=&quote;Reset&quote; value=&quote;Reset&quote;>
      </div></td>
    </tr>
</table>
</FORM>
<script>
if ( smatotal() > 0) {
document.write('You need to order ' +smatotal()+ ' SMAs');
} else {
document.write('<p>Enter your license quantities above to calculate the correct SMA combinations to order.</p>');
}

if ( addsmatotal() > 0) {
document.write('<p>You need to order ' +addsmatotal()+ ' additional SMA licenses</p>');
}

</script>
</CENTER>
</body>
</html>
:x

Posted: Thu Jul 28, 2005 3:58 pm
by pickle
What part isn't working?

Go here it might help clean up the code: JSLink: the Javascript verifier

Posted: Fri Jul 29, 2005 2:34 am
by hairyjim
Hi.

I failed to mention that when the page loads in Safari all is as expected. All fields are blank, the message "Please enter..." is visible and all looks fine. The problem is when a user has entered their values and they click 'calculate' it does not calculate the licenses they need to purchase. It seems to do a complete reload of the whole page.

My intention for the script was when the page is 'refreshed' the values of the fields are passed into the functions and are outputted to the screen.

Posted: Fri Jul 29, 2005 9:58 am
by pickle
It's reloading because you've told it to :). Your onClick calls code to reload the window. In order to make it work, you'll need to not use an onClick call, but an onSubmit call. Put that in the form tag like this:

Code: Select all

<form name = ..... onSubmit = 'addsmatotal();return(false);'>
That will run addsmatotal() when the form is submitted, then return false, which stops the form from being submitted.

Posted: Fri Jul 29, 2005 10:30 am
by hairyjim
Thanks for the help.

The JScript I did was rubbish, so I looked into how to do things properly and did it another way which worked.

Cheers
Jim