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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31281787/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 13:34:56  来源:igfitidea点击:

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

javascript

提问by sajjad_safari

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?

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



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

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

have any idea about it?

有什么想法吗?

回答by Carlos Huggins

My 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.

我的解决方案在当前链接上非常简单我已经有一个内联 onclick 事件定义,例如:onclick="window.open(....)",我只是在“.open()”之后添加“.print()”为了在加载 URL 后自动显示打印对话框,我已经在 chrome 和 firefox for linux 上进行了测试,并且按我的预期工作。

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

希望这是你正在寻找的

回答by Johnroe Paulo Ca?amaque

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:我所做的是隐藏不通过 CSS 媒体打印的元素。

1.php

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

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

2.php

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