Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing a web page using just url and without opening new window?

<html> <head> <script type="text/javascript">     var URL = "http://localhost:8000/foobar/";     var W = window.open(URL);    **Note1**     W.window.print();   </script> </head> <p> Print ME...............</p> </html> 

I am using this script to print a webpage. My views render this page and The JS take care all other things.

But I dont want to open new window for that. So, What should I use instead of window.open(URL) so no new window opens. Similarly, I don't want to open new window for print function.So, Whenever I render this page it do all stuff on the same page. No new window, No new tab. How can I achieve this. I google but nothing seems working.

like image 777
Tauquir Avatar asked Nov 23 '11 10:11

Tauquir


People also ask

How do I print a Web page without opening a popup window?

Just use window. print(); directly on the page. By itself it does not open a new window (apart from the print properties window to set printing options).

How do I print just the content of a Web page?

Just select the desired text on the current page and press CTRL+P. This will bring up the Print dialog, where you can simply select the "Selection" option there. This will print out only the selected text.

How do I print a Web page without URL?

Google Chrome: Go to the Menu icon in the top right corner of the browser and Click on Print button. Uncheck the “Headers and footers” option underneath the “Margins” option. Apple Safari: Go to the print option from the menu and the Print dialog appears. Uncheck the “Print headers and footers” option.


2 Answers

You can do this using a hidden iFrame (I'm using jquery for the example):

function loadOtherPage() {      $("<iframe>")                             // create a new iframe element         .hide()                               // make it invisible         .attr("src", "/url/to/page/to/print") // point the iframe to the page you want to print         .appendTo("body");                    // add iframe to the DOM to cause it to load the page  } 

This will load the page you want to print. To print, you can add javascript code to the print page so that it gets printed after loading:

$(document).ready(function () {     window.print(); }); 

This will print the page without showing a new window. I've tested this in IE8,9 and Google Chrome, so I'm not sure if this works for Safari or Firefox, though.

like image 103
andragon Avatar answered Oct 11 '22 07:10

andragon


There's a nice example on MDN how to do that with a hidden iframe https://developer.mozilla.org/en-US/docs/Printing#Print_an_external_page_without_opening_it

like image 26
Klemen Tusar Avatar answered Oct 11 '22 07:10

Klemen Tusar