Please help here are my two files: calcfactorialtopframe.htm <html> <head> <title>Example</title> <script type ="text/javascript"> function calcFactorial(factorialNumber) { var factorialResult = 1; for (; factorialNumber > 0; factorialNumber--) { factorialResult = factorialResult * factorialNumber; } return factorialResult; } </script> </head> <frameset cols="100%,*"> <frame name=fraCalcFactorial" src="calcfactorial.htm" /> </frameset> </html> Code (markup): calcfactorial.htm <html> <head> <title>Example</title> <script type="text/javascript"> function butCalculate_onclick() { try { if (window.top.calcFactorial == null) throw "This page is not loaded within the correct frameset"; if (document.form1.txtNum1.value == "") throw "!Please enter a value before you calculate its factorial"; if (isNaN(document.form1.txtNum1.value)) throw "!Please enter a valid number"; if (document.form1.txtNum1.value < 0) throw "!Please enter a positive number"; document.form1.txtResult.value = window.parent.calcFactorial(document.form1.txtNum1.value); } catch(exception) } if (typeof(exception) == "string") } if (exception.charAt(0) == "!") { alert(exception.substr(1)); document.form1.txtNum1.focus(); document.form1.txtNum1.select(); } else { alert(exception); } } else { alert("The following error occured " + exception.message); } } } </script> </head> </body> <form action="" name="form1"> <input type="text" name="txtNum1" size="3" /> factorial is <input type="text" name="txtResult" size="25" /><br /> <input type="button" value="Calculate Factorial" name="butCalculate" onclick="butCalculate_onclick()" /> </form> </body> </html> Code (markup): When I launch the first one and in the first box when I put 4 it should give me a result in the second box of 24.. but when I put a number in the first box the second box doesn't give me the results so I kept on checking the script but couldn't find the error, any help would be appreciated.
Change the file calcfactorial.htm to: <html> <head> <title>Example</title> <script type="text/javascript"> function butCalculate_onclick() { try { if (window.top.calcFactorial == null) throw "This page is not loaded within the correct frameset"; if (document.form1.txtNum1.value == "") throw "!Please enter a value before you calculate its factorial"; if (isNaN(document.form1.txtNum1.value)) throw "!Please enter a valid number"; if (document.form1.txtNum1.value < 0) throw "!Please enter a positive number"; document.form1.txtResult.value = window.parent.calcFactorial(document.form1.txtNum1.value); } catch(exception) { if (typeof(exception) == "string") { if (exception.charAt(0) == "!") { alert(exception.substr(1)); document.form1.txtNum1.focus(); document.form1.txtNum1.select(); } else { alert(exception); } } else { alert("The following error occured " + exception.message); } } } </script> </head> <body> <form action="" name="form1"> <input type="text" name="txtNum1" size="3" /> factorial is <input type="text" name="txtResult" size="25" /><br /> <input type="button" value="Calculate Factorial" name="butCalculate" onclick="butCalculate_onclick()" /> </form> </body> </html> Code (JavaScript):
yeh it works now but since I am doing this as a new starter can you tell me what was wrong with mine ?
The red lines are the wrong lines. Compare it with my version and you'll see. <html> <head> <title>Example</title> <script type="text/javascript"> function butCalculate_onclick() { try { if (window.top.calcFactorial == null) throw "This page is not loaded within the correct frameset"; if (document.form1.txtNum1.value == "") throw "!Please enter a value before you calculate its factorial"; if (isNaN(document.form1.txtNum1.value)) throw "!Please enter a valid number"; if (document.form1.txtNum1.value < 0) throw "!Please enter a positive number"; document.form1.txtResult.value = window.parent.calcFactorial(document.form1.txtNum1.value); } catch(exception) [COLOR="red"]}[/COLOR] if (typeof(exception) == "string") [COLOR="red"]}[/COLOR] if (exception.charAt(0) == "!") { alert(exception.substr(1)); document.form1.txtNum1.focus(); document.form1.txtNum1.select(); } else { alert(exception); } } else { alert("The following error occured " + exception.message); } } } </script> </head> [COLOR="red"]</body>[/COLOR] <form action="" name="form1"> <input type="text" name="txtNum1" size="3" /> factorial is <input type="text" name="txtResult" size="25" /><br /> <input type="button" value="Calculate Factorial" name="butCalculate" onclick="butCalculate_onclick()" /> </form> </body> </html> Code (markup):