Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can you add parameters inside a print() function in javascript

Tags:

javascript

I am looking for a way to add parameters on a print function, because I have to print only the table and when I alert the table it shows me the correct value but when I'm printing it prints the entire page.

My code is

aa = document.getElementById('tablename').innerHTML

If I alert(aa) it gives me the write value then I print(aa) it give me the entire page. so I tried print(aa) and aa.print and it doesn't work.

Does anyone know the solution for this?

like image 643
Dee-M Avatar asked May 20 '09 11:05

Dee-M


2 Answers

Print stylesheets are nice, but you can still accomplish this in Javascript. Just pass your value to be printed to the following function...

function printIt(printThis) {
  var win = window.open();
  self.focus();
  win.document.open();
  win.document.write('<'+'html'+'><'+'body'+'>');
  win.document.write(printThis);
  win.document.write('<'+'/body'+'><'+'/html'+'>');
  win.document.close();
  win.print();
  win.close();
}
like image 151
Josh Stodola Avatar answered Sep 21 '22 18:09

Josh Stodola


Define a print stylesheet which will only display the table.

  • http://www.webcredible.co.uk/user-friendly-resources/css/print-stylesheet.shtml
  • http://www.alistapart.com/articles/goingtoprint/

There's no need for it to be dynamic.

Simply define those sections you don't wan to see as display:none (as stated in the alistapart article)

like image 20
cgp Avatar answered Sep 20 '22 18:09

cgp