Printing contents of another page

女生的网名这么多〃 提交于 2019-12-18 15:16:09

问题


In the following link

<a href=\"#\" onclick=javascript:print(\"\") style=\"color:blue;\">Print</a>"
<script>
 function print()
 {
      //How to print the contents of another page
 }

回答1:


I think you can print the contents of the current page not external page.




回答2:


I know it´s an old question, but you can do it like this:

function printExternal(url) {
    var printWindow = window.open( url, 'Print', 'left=200, top=200, width=950, height=500, toolbar=0, resizable=0');
    printWindow.addEventListener('load', function(){
        printWindow.print();
        printWindow.close();
    }, true);
}

Tested in Firefox and Chrome. IE9 doesn´t work.




回答3:


If you already have an external page( letterprint.php ), put that page in a hidden iframe and print the content of iframe by using onclick attribute in a button.

<iframe src="letterprint.php" style="display:none;" name="frame"></iframe>

<input type="button" onclick="frames['frame'].print()" value="printletter">



回答4:


An alternative is to link to the page with a get variable and then call the print function.

For your link -

<a href="print-page.php?print=1">Print other page</a>

Then on your print page (or all pages)

<script type="text/javascript">
<? if(isset($_GET['print'])) { ?>
window.print();
<? } ?>
</script>



回答5:


You can print external page by creating window object of another page and calling print method of that page.
URL can be passed in argument or can be hardcoded in function depends on use case

function print(url) {
    var printWindow = window.open( url);
    printWindow.print();
};



回答6:


Think about the security/embarrassment issues that would exist if this was possible. Thankfully, browsers won't allow you to do that.

The closest you can get is fetching the page via AJAX, replacing the current DOM with the new page, and printing with JS's normal print() method.



来源:https://stackoverflow.com/questions/2578052/printing-contents-of-another-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!