I want to show a url content in new popup window and after it instantly show the print window to print the content of that url, may I ask you that how can I do this?
I edit my question: Really I want to open a popup window, load a specific div of a given url (for print friendly view of a post) and after load finished, open the print window...
have any idea about it?
I want to show a url content in new popup window and after it instantly show the print window to print the content of that url, may I ask you that how can I do this?
I edit my question: Really I want to open a popup window, load a specific div of a given url (for print friendly view of a post) and after load finished, open the print window...
have any idea about it?
Share Improve this question edited Jul 8, 2015 at 2:01 AstroCB 12.4k20 gold badges59 silver badges74 bronze badges asked Jul 8, 2015 at 0:48 sajjad_safarisajjad_safari 871 gold badge2 silver badges11 bronze badges 6- stackoverflow./questions/2603465/… – jpcanamaque Commented Jul 8, 2015 at 0:50
- Johnroe Thanks for your link, i updated my question,i read your link but not realized what to do about it, please guide me, Thank you so much – sajjad_safari Commented Jul 8, 2015 at 1:15
- is this url that you want to open in new window part of your website? if not, you are better off using your backend to grab that "portion" you want to print, and present in your front end – Ji_in_coding Commented Jul 8, 2015 at 1:17
-
If you have thoroughly read the answer on the link that I gave, All you have to do is have your page (the one that needs to be printed) execute
window.print()
on anonload
event. – jpcanamaque Commented Jul 8, 2015 at 1:19 - yes, in fact in my home page, i want to show a print icon for printing the post page, but as you know it will print existing document not linked post page, because of this i want to open that page first(and not whole the page and only a specisic div to have print frienly view, and after that load finished, print window appears... – sajjad_safari Commented Jul 8, 2015 at 1:20
2 Answers
Reset to default 5My solution is quite simple on a current link I already have an inline onclick event definition like: onclick="window.open(....)", I just add ".print()" after ".open()" in order to automatically show printing dialog after loading the URL, I have tested on chrome and firefox for linux and works as I expected.
My code looks something like this:
<a class="btn btn-success" href="#" onclick="window.open('URL_TO_POST','POPUP WINDOW TITLE HERE','width=650,height=800').print()">Print</a>
Hope this is what you are looking for
If I understand your question clearly. Here is a simple example:
EDIT 1 : What I did is hide the elements that are not to be printed through CSS media.
1.php
<html>
<script>
function sample() {
var x = document.getElementById('txt').value;
window.open('2.php?x='+x);
}
</script>
<input type = "text" id = "txt" />
<button onclick = "sample();"> Sample </button>
</html>
2.php
<html>
<style type="text/css">
@media print
{
#not-print {display:none;}
}
</style>
<body onload = "window.print()">
<div id = 'print'>
<?php
if(isset($_GET['x'])) {
echo $_GET['x'];
} else {
echo 'Hello World';
}
?>
</div>
<div id = 'not-print'>
This is not printed
<div>
</body>
<html/>
EDIT 2 : See code below:
2.php
<html>
<style type="text/css">
@media print
{
#not-print {display:none;}
}
</style>
<body>
<div id = 'print'>
This should be printed!
</div>
<div id = 'not-print'>
This is not printed
<div>
<button onclick = "printdiv()">Print Div </button>
<script>
function printdiv() {
var mywindow = window.open("", '_blank');
mywindow.document.write('<p>' + document.getElementById('print').innerHTML + '</p>');
mywindow.print();
}
</script>
</body>
<html/>