2

Is there a way to for a parent to know when a child window has loaded it's content? Without any script in the child.

Here is my current code:

var printWindow = window.open('', '_blank');
printWindow.document.write(clonedElement.html());
printWindow.onreadystatechage = function() {
    console.log('this does not work.');
};
4

2 回答 2

2

你有相同的事件触发,所以如果你打开页面已经有 html window,你可以得到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;
于 2015-07-30T15:08:30.020 回答
0

看来您可能在这里面临跨域策略问题。父页面和子页面必须托管在同一个域中。浏览器,例如 chrome,不会允许它,因为 FF 在某些情况下可能更宽松。您应该查看“启用 CORS”,此链接也应该对您有所帮助: 等待子窗口加载完成

于 2015-07-30T15:16:57.507 回答