在新窗口中打开链接,然后立即显示打印窗口

Open a link in new window and then show print window instantly

本文关键字:显示 打印 窗口 链接 然后 在新窗口中打开      更新时间:2023-09-26

我想在新的弹出窗口中显示一个url内容,并在它立即显示打印窗口后打印该url的内容,我可以问你,我怎么能做到这一点?


我编辑我的问题:真的我想打开一个弹出窗口,加载给定url的特定div(用于帖子的打印友好视图),加载完成后,打开打印窗口…

有什么想法吗?

我的解决方案是相当简单的在当前链接上,我已经有一个内联的onclick事件定义,如:onclick="window.open(....)",我只是在".open()"之后添加".print()",以便在加载URL后自动显示打印对话框,我已经在chrome和firefox上测试了linux和工作如我所期望的。

我的代码看起来像这样:

<a class="btn btn-success" href="#" onclick="window.open('URL_TO_POST','POPUP WINDOW TITLE HERE','width=650,height=800').print()">Print</a>

希望这是你正在寻找的

如果我明白你的问题。下面是一个简单的例子:


EDIT 1:我所做的是隐藏不通过CSS媒体打印的元素。

php 1.

<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>
php 2.

<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:参见下面的代码:

php 2.

<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/>