Javascript 如何使用chrome打包应用程序在子窗口中写入内容?
我已经使用chrome.app.window.create API创建了新的子窗口,以便使用window.print()打印窗口的内容。但我的问题是如何在该html中传递内容。请有人帮助我。可以通过Javascript 如何使用chrome打包应用程序在子窗口中写入内容?,javascript,html,gwt,google-chrome-app,Javascript,Html,Gwt,Google Chrome App,我已经使用chrome.app.window.create API创建了新的子窗口,以便使用window.print()打印窗口的内容。但我的问题是如何在该html中传递内容。请有人帮助我。可以通过window.document访问新窗口的DOM 这里的窗口不是传递给chrome.app.window.create回调函数(称之为createdWindow)的窗口,而是createdWindow.contentWindow 因此,您要查找的DOM是createdWindow.contentWin
window.document
访问新窗口的DOM
这里的窗口不是传递给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();
}
}
);
};
window.onload=function(){
create('printwindow.html',{},
函数(createdWindow){
var win=createdWindow.contentWindow;
win.onload=函数(){
win.document.querySelector(“#content”).innerHTML=
“这里有一些东西要打印。”;
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){u-win.contentWindow.document.write('this is test content');_-win.contentWindow.print();});首先尝试使用
document.open()
。当我使用document.open()时,它说,打包的app.Xan中没有document.write;它的抛出异常(如document.open())在打包的应用程序中不可用。请在下面找到我的代码:chrome.app.window.create('Printwindow.html',function(_-win){u-win.contentWindow.document.open();_-win.contentWindow.document.write('mansoor');_-win.contentWindow.print());请帮助我..无法写入内容。as document.write()是打包应用程序中禁用的功能。