小编典典

仅使用网址而不打开新窗口来打印网页?

html

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

我正在使用此脚本来打印网页。我的视图呈现了此页面,而JS会照顾所有其他事情。

但是我不想为此打开新窗口。因此,我应该使用什么代替window.open(URL)so no new windowopen。同样,我也不想为。打开新窗口,print function所以,每当我渲染此页面时,它都会在同一页面上执行所有操作。没有新窗口,没有新标签。我该如何实现。我谷歌,但似乎没有任何工作。


阅读 466

收藏
2020-05-10

共1个答案

小编典典

您可以使用隐藏的 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

}

这将加载您要打印的页面。要进行打印,可以将javascript代码添加到打印页面,以便在加载后将其打印出来:

$(document).ready(function () {
    window.print();
});

这将打印页面而不显示新窗口。我已经在IE8,9和Google Chrome浏览器中对此进行了测试,因此我不确定这是否适用于Safari或Firefox。

2020-05-10