有没有办法让父窗口知道子窗口何时加载其内容?子进程中没有任何脚本。
这是我当前的代码:
var printWindow = window.open('', '_blank');
printWindow.document.write(clonedElement.html());
printWindow.onreadystatechage = function() {
console.log('this does not work.');
};
最佳答案
您在 window
上触发的事件相同,因此如果您打开已经包含 html 的页面,则可以获得 load 事件,例如
var b = new Blob([clonedElement.html()], {type: 'text/html'}),
uri = URL.createObjectURL(b),
printWindow;
printWindow = window.open(uri, '_blank');
printWindow.addEventListener('load', function () {
URL.revokeObjectURL(uri); // release these
b = null; // from memory
// do something else
});
请注意,这里会有一个竞争条件(尽管您几乎总是会赢得它,即如果页面加载并且事件在打开和添加监听器之间触发)
或者,没有比赛
var b = new Blob([clonedElement.html()], {type: 'text/html'}),
uri = URL.createObjectURL(b),
printWindow = window.open('', '_blank');
printWindow.addEventListener('load', function () {
URL.revokeObjectURL(uri); // release these
b = null; // from memory
// do something else
});
printWindow.location = uri;
关于Javascript 窗口写入回调,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31727123/