<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>
Run Code Online (Sandbox Code Playgroud)
我正在使用此脚本打印网页.我的观点呈现了这个页面,而JS则关注所有其他事情.
但我不想为此打开新窗口.所以,我应该用什么来代替window.open(URL)
这样no new window
打开.同样,我不想为print function
.So 打开新窗口.每当我渲染这个页面时,它都会在同一页面上执行所有操作.没有新窗口,没有新标签.我怎样才能做到这一点.我谷歌但似乎没有任何工作.
小智 44
您可以使用隐藏的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
}
Run Code Online (Sandbox Code Playgroud)
这将加载您要打印的页面.要打印,您可以将javascript代码添加到打印页面,以便在加载后打印:
$(document).ready(function () {
window.print();
});
Run Code Online (Sandbox Code Playgroud)
这将打印页面而不显示新窗口.我已经在IE8,9和谷歌浏览器中对此进行了测试,但我不确定这是否适用于Safari或Firefox.
在MDN上有一个很好的例子如何使用隐藏的https://developer.mozilla.org/en-US/docs/Printing#Print_an_external_page_without_opening_it来做到这一点iframe