Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5
  1. #1
    New to the CF scene
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation Please help cant get print to work

    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

  • #2
    Regular Coder
    Join Date
    Jan 2006
    Posts
    568
    Thanks
    6
    Thanked 84 Times in 84 Posts
    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.

  • #3
    New to the CF scene
    Join Date
    May 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    could you suggest a change to this program becuase im not sure i have tried everything to no avail

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,159
    Thanks
    203
    Thanked 2,548 Times in 2,526 Posts
    Quote Originally Posted by tyson642 View Post
    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

    All the code given in this post has been tested and is intended to address the question asked.
    Unless stated otherwise it is not just a demonstration.

  • #5
    Banned
    Join Date
    Feb 2011
    Posts
    2,699
    Thanks
    13
    Thanked 395 Times in 395 Posts
    Quote Originally Posted by tyson642 View Post
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •