【问题标题】:How to write content in child window using chrome packaged app?如何使用 chrome 打包的应用程序在子窗口中写入内容?
【发布时间】:2014-08-25 13:55:42
【问题描述】:

我已经使用 chrome.app.window.create API 创建了新的子窗口,以便使用 window.print() 打印窗口的内容。但我的问题是如何传递该 html 中的内容。请任何人都可以帮助我。

【问题讨论】:

    标签: javascript html gwt google-chrome-app


    【解决方案1】:

    新窗口的 DOM 可通过window.document 访问。

    这里的窗口不是传递给chrome.app.window.create回调函数的窗口(称为createdWindow),而是createdWindow.contentWindow。

    所以,您要查找的 DOM 是 createdWindow.contentWindow.document。随意修改该 DOM,然后调用 createdWindow.contentWindow.print()。

    更新 -- 下面是我上面所说的代码:

    window.onload = function() {
      chrome.app.window.create('printwindow.html', {},
        function (createdWindow) {
          var win = createdWindow.contentWindow;
          win.onload = function () {
            win.document.querySelector('#content').innerHTML =
              '<p>Here is something to print.</p>';
            win.print();
          }
        }
      );
    };
    

    printwindow.html 是:

    <!DOCTYPE html>
    <html>
      <head>
        <title>WindowToPrint</title>
      </head>
      <body>
        <div id="content"></div>
      </body>
    </html>
    

    我已经测试了这段代码,它可以工作。我知道 OP 采取了另一种方式,但如果可以将其标记为答案,那么对于其他查看此问题的人来说会很好。

    【讨论】:

    • 如何修改该 DOM ?我试图在上面写内容,比如 chrome.app.window.create('Printwindow.html', function(_win) { _win.contentWindow.document.write('this is test content'); _win.contentWindow.print ();});
    • 先尝试使用document.open()。
    • 它说,document.write 在打包的应用程序中不可用。
    • Xan,当我使用 document.open() 时;像 document.open() 这样的抛出异常在打包的应用程序中不可用。请在我的代码下面找到:chrome.app.window.create(' Printwindow.html', function(_win) { _win.contentWindow.document.open(); _win.contentWindow.document.write('mansoor'); _win.contentWindow.print(); });
    • 请帮帮我..无法编写内容。因为 document.write() 是打包应用程序中的禁用功能。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-11
    • 1970-01-01
    • 2012-12-02
    • 2014-02-18
    • 1970-01-01
    • 1970-01-01
    • 2013-11-26
    相关资源
    最近更新 更多