...

View Full Version : Please help cant get print to work



tyson642
06-01-2011, 05:38 PM
i have tried everything, you can see the print function when the program starts yet as soon as it finishes print has vanished

why is this

also when the program displays the result the variable displays the correct item except it has undefined next to it:

here is my code please help:

<html>
<head>

<title>Book Program</title>

<script type="text/javascript">

var book = new Array()
var price = new Array();
var total = 0;

//this shows the prompt which allows the user to enter the information
function show_prompt(){
//this says that the variable i = o
var i=0
//this asks how many books have you sold and it then asks for the number of books depending on how many you entered
booknum = parseFloat(prompt('how many books have you sold today'));
for (i = 0; i < booknum; i++){
book[i] = prompt("what is the name of the book?");
//this asks you to enter the price of the items from the book
price[i] = parseFloat(prompt('Enter price of item named :'+book[i]));
//this displays the name and price of book on screen
document.write("<br>"+"name:" + ' ' + book[i] + " " + "Price:" + price[i] + '<br/>' );
//this totals the price variable
total+=parseFloat(price[i]);
}
document.write("<br>"+"Total Cost: " + total ); //displays the results
}

</script>

</head>

<body>
<body onload = "document.write(show_prompt())">
<form><input type="button" value=" Print Page "
onclick="window.print();return false;" /></form>

</body>

</html>
Reply With Quote

gusblake
06-01-2011, 08:08 PM
Calling document.write after the page has loaded causes everything on the page -- including any script tags -- to be replace by whatever you're printing. I think this is the cause of your problems.

tyson642
06-01-2011, 08:43 PM
could you suggest a change to this program becuase im not sure i have tried everything to no avail

Philip M
06-02-2011, 08:15 AM
could you suggest a change to this program becuase im not sure i have tried everything to no avail

Create a <span> or <div> and assign an id to it for the output message, then use document.getElementById("myDiv").innerHTML = (the content you wish to display)

As gusblake says, document.write statements must be run before the page finishes loading. Any document.write statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page. So document.write is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.


Quizmaster: In 2006 the Star Wars character voted the most annoying film character ever was Jar Jar ....what?
Contestant: Gabor

bullant
06-02-2011, 08:24 AM
could you suggest a change to this program becuase im not sure i have tried everything to no avail

You can use DOM methods to write output to a hrml element.

In this case element options include a <p>, <div>, <span> provided it's inside an appropriate block level element, <label> etc.

document.write only works on the current page while it is still loading. Otherwise it can be used to write content to a newly opened second window.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum