That's what it does. In fact, it should be doing it at line 30.
In any case, whenever you use document.write or document.writeln, that will happen unless the page is still being parsed. Once the page has loaded, write and writeln both overwrite the existing content. It's not really a new page. It has some of the characteristics of a new page though. It is new content for the window.
What you may want to do is to write to an element like a division by using its innerHTML property or by altering it via DOM level 2 methods. The latter is preferred but is generally more complicated.
You have another thing going on that may cause a problem even if you use the DOM or innerHTML. Your form will probably submit. The way it is currently setup, that would reload the page, losing the calculations just made.
This will work, though some tweaks may be desired. I've altered both the script and the form as well as made a few minor corrections:
Code:
<html>
<head>
<title>JS input example.</title>
<script type="text/javascript">
/* Start of round_total function.
*/
function round_total (c) {
var pennies = c * 100;
pennies = Math.round(pennies);
var strPennies = "" + pennies;
var len = strPennies.length;
return parseFloat(strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len));
}
// End of round_total function.
/* Start of generate_page function. */
function generate_page (form) {
tax = 0.08;
delivery_p = 2.99;
var odate = new Date();
var qty = form.quantity.value;
var product_v = new String(form.product.value);
var total_price = product_v.substr(product_v.indexOf("$") + 1, product_v.length - product_v.indexOf("$"));
var price_without_tax = round_total(qty * total_price);
var ttax = round_total(price_without_tax * tax);
var delivery = round_total(qty * delivery_p);
var total_p = round_total(price_without_tax + ttax + delivery);
var resultHTML = "Quantity: " + qty + "<br>\n" +
"Price: $" + total_price + "<br>\n" +
"Tax: $" + ttax + "<br>\n" +
"Delivery: $" + delivery + "<br>\n" +
"Total: $" + total_p + "<br>\n" +
"Order placed on: " + odate.toGMTString();
document.getElementById('result').innerHTML = resultHTML;
}
// End of generate_page function.
</script>
</head>
<body>
<form action="#" onsubmit="generate_page(this); return false;">
<p>Product: <select name="product" size="1">
<option value="Brown Jacket - Size 35 - $58.99">Brown Pants - Size 31 - $58.99</option>
<option value="Blue Shirt - Size 32 - $69.99">Blue Shirt - Size 32 - $69.99</option>
</select><br>
Quantity: <input type="text" name="quantity" size="18" value="1"><br>
<input type="submit" value="Submit" name="submit"><input
type="reset" value="Reset" name="reset"></p>
</form>
<div id="result">
</div>
</body>
</html>
If you still want to overwrite the page, but only do so once:
Code:
<html>
<head>
<title>JS input example.</title>
<script type="text/javascript">
/* Start of round_total function.
*/
function round_total (c) {
var pennies = c * 100;
pennies = Math.round(pennies);
var strPennies = "" + pennies;
var len = strPennies.length;
return parseFloat(strPennies.substring(0, len - 2) + "." + strPennies.substring(len - 2, len));
}
// End of round_total function.
/* Start of generate_page function. */
function generate_page (form) {
tax = 0.08;
delivery_p = 2.99;
var odate = new Date();
var qty = form.quantity.value;
var product_v = new String(form.product.value);
var total_price = product_v.substr(product_v.indexOf("$") + 1, product_v.length - product_v.indexOf("$"));
var price_without_tax = round_total(qty * total_price);
var ttax = round_total(price_without_tax * tax);
var delivery = round_total(qty * delivery_p);
var total_p = round_total(price_without_tax + ttax + delivery);
var resultHTML = "Quantity: " + qty + "<br>\n" +
"Price: $" + total_price + "<br>\n" +
"Tax: $" + ttax + "<br>\n" +
"Delivery: $" + delivery + "<br>\n" +
"Total: $" + total_p + "<br>\n" +
"Order placed on: " + odate.toGMTString();
document.write(resultHTML);
}
// End of generate_page function.
</script>
</head>
<body>
<form action="#" onsubmit="generate_page(this); return false;">
<p>Product: <select name="product" size="1">
<option value="Brown Jacket - Size 35 - $58.99">Brown Pants - Size 31 - $58.99</option>
<option value="Blue Shirt - Size 32 - $69.99">Blue Shirt - Size 32 - $69.99</option>
</select><br>
Quantity: <input type="text" name="quantity" size="18" value="1"><br>
<input type="submit" value="Submit" name="submit"><input
type="reset" value="Reset" name="reset"></p>
</form>
</body>
</html>
Bookmarks