【问题标题】:Printing a web page using just url and without opening new window?仅使用 url 打印网页而不打开新窗口?
【发布时间】:2012-01-04 15:26:55
【问题描述】:
<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) 所以 no new window 打开。同样,我不想为print function 打开新窗口。所以,每当我呈现此页面时,它都会在同一页面上执行所有操作。没有新窗口,没有新标签。我怎样才能做到这一点。我用谷歌搜索,但似乎没有任何效果。

【问题讨论】:

    标签: javascript html


    【解决方案1】:

    我正在使用带有 razor html 作为视图的 Asp .net 核心,在这种情况下,我使用 window.print() 打印页面,然后使用 window.onafterprint 返回到使用过的想要重定向的页面。

    您可以使用 ViewBag 替换“/NewSales” URL。

    注意:只要用户点击弹出窗口中的取消/提交/打印按钮,就会调用 window.onafterprint

    $(document).ready(function () {
        window.print();
    
        window.onafterprint = function () {
            window.location.href = "/NewSales";
        }
    });
    

    【讨论】:

      【解决方案2】:

      参考@andragon's answer。在此基础上更新。

      您可以使用 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");
          }
      

      这将加载您要打印的页面链接。 在加载打印页面链接时,您可以调用 javascript。

      $(document).ready(function () {
              window.print();
          });
      
          window.onafterprint = function () {
              $('.printpage', window.parent.document).hide();
          }
      

      这将从同一个窗口打印页面,并且当页面开始打印或打印对话框已关闭时触发 onafterprint 事件

      window.parent.document是隐藏父页面的iFrame块。

      【讨论】:

        【解决方案3】:

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

        【讨论】:

        • 在“/url/to/page/to/print”,可以直接放我的html代码吗?
        • @VitorGuerreiro:是的,您要打印的 html 代码应该在那个 url;该html代码还应该具有答案中提到的javascript代码(带有 window.print(); 的那个)
        • 隐藏的 iFrame 在最新版本的浏览器中打印空白页。
        • 是的空白页隐藏,将.hide()替换为.css('visibility', 'hidden')
        【解决方案4】:

        在 MDN 上有一个很好的例子,如何使用隐藏的 iframe https://developer.mozilla.org/en-US/docs/Printing#Print_an_external_page_without_opening_it 做到这一点

        【讨论】:

          【解决方案5】:
           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();
              }
          

          在单击打印按钮时调用此 javascript 函数。“main”是我们必须在不打开新窗口的情况下打印的 div 的 id。我想通知这将打印当前页面 div。 如果出现任何问题,请尝试并恢复。
          谢谢,
          古拉夫

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-11-06
            • 2011-01-12
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2018-05-02
            • 2018-10-29
            • 1970-01-01
            相关资源
            最近更新 更多