Javascript 仅使用 url 打印网页而不打开新窗口?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8240472/
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 a web page using just url and without opening new window?
提问by Tauquir
<html>
<head>
<script type="text/javascript">
var URL = "http://localhost:8000/foobar/";
var W = window.open(URL); **Note1**
W.window.print();
</script>
</head>
<p> Print ME...............</p>
</html>
I am using this script to print a webpage. My views render this page and The JS take care all other things.
我正在使用此脚本打印网页。我的视图呈现此页面,而 JS 负责所有其他事情。
But I dont want to open new window for that. So, What should I use instead of window.open(URL)so no new windowopens. Similarly, I don't want to open new window for print function.So, Whenever I render this page it do all stuff on the same page. No new window, No new tab. How can I achieve this. I google but nothing seems working.
但我不想为此打开新窗口。那么,我应该使用什么而不是window.open(URL)这样no new window打开。同样,我不想为print function.So打开新窗口,每当我呈现此页面时,它都会在同一页面上执行所有操作。没有新窗口,没有新标签。我怎样才能做到这一点。我谷歌但似乎没有任何工作。
回答by andragon
You can do this using a hidden iFrame(I'm using jquery for the example):
您可以使用隐藏的iFrame执行此操作(我使用 jquery 作为示例):
function loadOtherPage() {
$("<iframe>") // create a new iframe element
.hide() // make it invisible
.attr("src", "/url/to/page/to/print") // point the iframe to the page you want to print
.appendTo("body"); // add iframe to the DOM to cause it to load the page
}
This will load the page you want to print. To print, you can add javascript code to the print page so that it gets printed after loading:
这将加载您要打印的页面。要打印,您可以将 javascript 代码添加到打印页面,以便在加载后打印:
$(document).ready(function () {
window.print();
});
This will print the page without showing a new window. I've tested this in IE8,9 and Google Chrome, so I'm not sure if this works for Safari or Firefox, though.
这将打印页面而不显示新窗口。我已经在 IE8,9 和 Google Chrome 中对此进行了测试,所以我不确定这是否适用于 Safari 或 Firefox。
回答by Klemen Tu?ar
There's a nice example on MDN how to do that with a hidden iframehttps://developer.mozilla.org/en-US/docs/Printing#Print_an_external_page_without_opening_it
MDN 上有一个很好的例子,如何使用隐藏的iframehttps://developer.mozilla.org/en-US/docs/Printing#Print_an_external_page_without_opening_it
回答by Shivakrishna
In reference to @andragon's answer. updated on top of it.
参考@andragon 的回答。在它上面更新。
You can do this using an iFrame(Not hidden because hidden iFrame prints the blank page in latest versions of browsers. You can hide after the print is triggered)
您可以使用 iFrame(不隐藏,因为隐藏的 iFrame 会在最新版本的浏览器中打印空白页。您可以在触发打印后隐藏)
function loadOtherPage(link) {
$("<iframe class='printpage'>") // create a new iframe element
.attr("src", link) // point the iframe to the page link you want to print
.appendTo("body");
}
This will load the page link you want to print. On loading the print page link you can call javascript.
这将加载您要打印的页面链接。在加载打印页面链接时,您可以调用 javascript。
$(document).ready(function () {
window.print();
});
window.onafterprint = function () {
$('.printpage', window.parent.document).hide();
}
This will print the page from the same window and onafterprint Event is triggered when a page has started printing, or if the print dialog box has been closed
这将从同一窗口打印页面,并且当页面开始打印时触发 onafterprint 事件,或者如果打印对话框已关闭
window.parent.documentis to hide the iFrame block on the parent page.
window.parent.document是隐藏父页面上的 iFrame 块。
回答by Gourav khanna
function CallPrint() {
var prtContent = document.getElementById('main');
var WinPrint = window.open('', '', 'width=800,height=650,scrollbars=1,menuBar=1');
var str = prtContent.innerHTML;
WinPrint.document.write(str);
WinPrint.document.close();
WinPrint.focus();
}
Call this javascript function on Print button click."main" is the id of the div which we have to print without opening into new window.I want to notify that this will print the current page div.
Try and rever in case of any issue.
Thanks,
Gourav
在“打印”按钮上调用此 javascript 函数单击。“main”是我们必须在不打开新窗口的情况下打印的 div 的 id。我想通知这将打印当前页面 div。如果出现任何问题,请尝试并恢复。
谢谢,
古拉夫

