Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

printing javascript dynamic data

Status
Not open for further replies.

BobTB

Technical User
Oct 28, 2002
37
AU
I am using a cookie based javascript shopping cart within a web site. After a 'buy' button is clicked on the sales page the data is displayed on the sales checkout page, showing quantity, price, total etc. in a javascript table. I want to be able to then print an order form but without a lot of the graphics and other text. Is there a way to write the shopping cart data into another printable page?
 
You can do this with style sheets instead of another window. Use the following:

<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;default.css&quot;>
<link rel=&quot;stylesheet&quot; type=&quot;text/css&quot; href=&quot;print.css&quot; media=&quot;print&quot; />

In default.css, add the style for web page as you normally would. In print.css, add style that you want when they print the page. The style in default.css will still be applied, but if their are classes or id's with identical names in both css documents, the print.css one will override the other.

I commonly put this in print.css:
.hideOnPrint{
display:none;
}

Then I add class=&quot;hideOnPrint&quot; to anything I don't want to show up on the printed version. Adam
 
Hi Adam0101, Thanks for the reply, it's good to know that i can control these with CSS but how would i do it if a want to do the printed page layout with slightly different design, e.g. Company Header, and other text that is not on the original salesorder form?

Many Thanks

BobTB
 
You can do it by opening popup window and writing there the checkout data. The idea is this:

function writeCheckout(data) {

a = window.open(&quot;&quot;,&quot;&quot;,&quot;width=200,height=200,toolbar=no,....&quot;)
a.document.open();
a.document.write(&quot;<html><head></head><body onload='print()'>&quot;);
a.document.write(data);
a.document.write(&quot;</body></html>&quot;);
a.document.close();
}

&quot;data&quot; is a string with text, or even html code for table. If you don't want to pass it as a parameter, do this:
a.document.write(&quot;<table><tr><td>quantity:</td><td>30</td></tr> ...&quot;);
 
Thanks Starway,

Can you clear up a question?
The shopping cart data is firstly written to the sales checkout page using the cookie based javascript. This page has graphics and other sales information on it. Once the shopping cart data is written into this page is it possible to then transfer the cart data, for example - Quantity, Price, Total etc. onto another printable page without including certain graphics and sales information?
 
Yes of course. Just do the same as for &quot;normal&quot; page - extract the data from cookie and write it to a new window using method I described, but without any graphics.
 
starway,
1. Do i place the script that you have supplied into the new web page?

2. Do I need to place the cookie javascript into the new page as well?

3. What code do i actually need to place into the data part of your script?

Sorry I am not very good with javascript

Thanks
 
The script should be placed in a page where you want to print from. If it is a checkout page - it should be there, and some &quot;Print order&quot; button should activate it:
<input type=&quot;button&quot; value=&quot;print order&quot; onclick=&quot;writeCheckout()&quot;>

The script should be in the <head> of this page. The &quot;new&quot; page (i.e. popup window) with all it's data will be created automatically.
This script should contain the code for cookie processing, looking like smth like this (without &quot;data&quot; parameter, it will be done internally):

function writeCheckout() {
// [ code for cookie processing ]
// let's assume that all data will be stored in a variable that you will print then:
var data = xxx; // something taken from the cookie

a = window.open(&quot;&quot;,&quot;&quot;,&quot;width=200,height=200,toolbar=no,....&quot;)
a.document.open();
a.document.write(&quot;<html><head></head><body onload='print()'>&quot;);
a.document.write(data);
a.document.write(&quot;</body></html>&quot;);
a.document.close();
}

I think this is the most simple way I could explain this.
 
Hi starway, do i have a html link on the sales checkout page that links to the new printable page?
 
Starway, I have a couple of questions about the script you have suggested.

var data = xxx; What text do I need to type in the 'XXX' part above?


a.document.write(data); Do I need to type anything between the brackets in the (data) section above?
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top