Javascript 打印另一页的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2578052/
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
Printing contents of another page
提问by Hulk
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
}
采纳答案by Sarfraz
回答by Alejandro Fiore
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.
在 Firefox 和 Chrome 中测试。IE9 不工作。
Edit 2020-04-03
编辑 2020-04-03
No longer work on Chrome, code adapted from Coder answer here:
不再在 Chrome 上工作,代码改编自Coder 在这里回答:
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() {
if (Boolean(printWindow.chrome)) {
printWindow.print();
setTimeout(function(){
printWindow.close();
}, 500);
} else {
printWindow.print();
printWindow.close();
}
}, true);
}
回答by 151291
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.
如果你已经有一个外部页面(letterprint.php),把那个页面放在一个隐藏的 iframe 中,并使用按钮中的 onclick 属性打印 iframe 的内容。
<iframe src="letterprint.php" style="display:none;" name="frame"></iframe>
<input type="button" onclick="frames['frame'].print()" value="printletter">
回答by sebtucknott
An alternative is to link to the page with a get variable and then call the print function.
另一种方法是使用 get 变量链接到页面,然后调用打印函数。
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>
回答by Rupesh Agrawal
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
您可以通过创建另一个页面的窗口对象并调用该页面的打印方法来打印外部页面。
URL 可以在参数中传递,也可以在函数中硬编码,具体取决于用例
function print(url) {
var printWindow = window.open( url);
printWindow.print();
};
回答by Amy B
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.
最接近的是通过 AJAX 获取页面,用新页面替换当前 DOM,并使用 JS 的普通 print() 方法打印。

