私たちの会社では、これ以上ない元同僚から次のコードが見つかりました:私は、我々のコードでsetTimeout
通話の量を減らしたいこのsetTimeoutをwindow.open()の後に削除できますか?
var w = window.open('', 'print_window', 'width = 1000, height = 1000');
w.document.write('<html><head><title>Printout</title>' + '<link rel="stylesheet" href="' + document.styleSheets[0].href + '"></head><body>');
w.document.write(modal.dom.root.innerHTML);
w.document.write('</body></html>');
setTimeout(function() {
$(w.document.body).find('.modal-print').remove();
w.document.close();
w.focus();
w.print();
w.close();
}, 500);
を、ここでsetTimeout
がある理由を疑問に思いました。私はそれを削除したいと思いますが、私はそれが最初に追加された理由があると思います。誰もがなぜそこに存在する可能性があるのかを知っていますか?それが安全に削除できるのであれば、コードはこのように見えますか?
var w = window.open('', 'print_window', 'width = 1000, height = 1000');
w.document.write('<html><head><title>Printout</title>' + '<link rel="stylesheet" href="' + document.styleSheets[0].href + '"></head><body>');
w.document.write(modal.dom.root.innerHTML);
w.document.write('</body></html>');
$(w.document.body).find('.modal-print').remove();
w.document.close();
w.focus();
w.print();
w.close();
私は、スタイルシートが読み込まれるのを待っていると思います。したがって、新しいウィンドウにコードを追加する前にwindow.open()
が呼び出されてから、スタイルシートが追加された後にこれらのイベントが発生するかどうかわかりませんが、DOMの準備完了イベントまたはロードイベントを待つのは良いでしょう。
readyイベントのバージョン:ロード・イベントと
var w = window.open('', 'print_window', 'width = 1000, height = 1000');
w.document.write('<html><head><title>Printout</title>' + '<link rel="stylesheet" href="' + document.styleSheets[0].href + '"></head><body>');
w.document.write(modal.dom.root.innerHTML);
w.document.write('</body></html>');
// Will ready event be fired? If yes, at the right time?
$(w.document).ready(function() {
$(w.document.body).find('.modal-print').remove();
w.document.close();
w.focus();
w.print();
w.close();
});
バージョン:
var w = window.open('', 'print_window', 'width = 1000, height = 1000');
w.document.write('<html><head><title>Printout</title>' + '<link rel="stylesheet" href="' + document.styleSheets[0].href + '"></head><body>');
w.document.write(modal.dom.root.innerHTML);
w.document.write('</body></html>');
// Will ready event be fired? If yes, at the right time?
$(w).load(function() {
$(w.document.body).find('.modal-print').remove();
w.document.close();
w.focus();
w.print();
w.close();
});
なぜテストしないのですか? –
@JonasGiuro私は現在、onloadのバージョンを試していますし、実装をテストするためにQAに依頼します。しかし、私はおそらく、SOの誰かがこれについての経験を既に持っていて、それがベストプラクティスであるかどうかを知っていると思いました。また、さまざまな環境のユーザーが異なる結果をもたらす可能性があることを心配しています。彼らのコンピュータが私たちがテストするのに使うものよりも遅いならば。 – Timo
は、そのウィンドウが開くのに時間がかかります – charlietfl