Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/449.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 是否可以在window.open()之后删除此设置超时?_Javascript_Settimeout - Fatal编程技术网

Javascript 是否可以在window.open()之后删除此设置超时?

Javascript 是否可以在window.open()之后删除此设置超时?,javascript,settimeout,Javascript,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].hre

我从一位不在我们公司的前同事那里找到了以下代码:

    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);
我猜他正在等待样式表加载。因此,等待DOM就绪或加载事件可能是一个很好的替代,尽管我不知道这些事件是否会在添加样式表后触发,因为在将代码添加到新窗口之前调用了
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>');

    // 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、 文件写入(“打印输出”+”;
w、 document.write(modal.dom.root.innerHTML);
w、 文件。写(“”);
//准备就绪事件将被触发吗?如果是,在正确的时间?
$(w.document).ready(函数(){
$(w.document.body).find('.modal print').remove();
w、 document.close();
w、 焦点();
w、 打印();
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();
    });
var w=window.open(“”,'print_window','width=1000,height=1000');
w、 文件写入(“打印输出”+”;
w、 document.write(modal.dom.root.innerHTML);
w、 文件。写(“”);
//准备就绪事件将被触发吗?如果是,在正确的时间?
$(w).加载(函数(){
$(w.document.body).find('.modal print').remove();
w、 document.close();
w、 焦点();
w、 打印();
w、 close();
});

仅供参考。我在评论中提到的代码,在遇到同样的问题后,我们最终使用了该代码:

function printHTML( html ) {
    var tab,
        interval;
    if (html) {
        tab = window.open($.ajax.host + $.ajax.directory + 'dist/res/print.html');
        interval = setInterval(function() {
            var body;
            // loop until the tab is opened, the tab has a document and the DOM has been parsed and is ready to be query'ed.
            if (tab && tab.document && tab.document.querySelector) body = tab.document.querySelector('body');
            if (body) {
                body.innerHTML = html.outerHTML;
                timeout again for the print function, so that the body has time to do a paint + reflow, so we are sure that the html has been written to the new tab.
                setTimeout(function() { 
                    // print and clean up
                    tab.print();
                    tab.close();
                    clearInterval(interval);
                }, 1)
            }
        }, 100);
    }
    else throw new Error("This report doesn't have a print version.");
}

与其在打印前使用
setTimeout
删除该节,不如在文档中添加以下样式

<style media="print">
  .modal-print {
    display: none;
  }
</style>

.模态打印{
显示:无;
}
这将隐藏该节,而不是删除它,但这可能是您所需要的全部

你也可以加上

<script>
  window.print();
  window.close();
</script>

window.print();
window.close();

那么你根本不需要设置超时,因为它都在你创建的文档中。

为什么不测试它们呢?@JonasGiuro我正在尝试onload版本,并要求QA测试实现。然而,我想也许有人已经有了这方面的经验,知道哪一个是最佳实践。此外,我担心不同环境中的用户可能会得到不同的结果,例如,如果他们的计算机比我们用来测试的计算机慢。打开该窗口需要时间我知道,无论如何,使用超时等待加载总是一个坏主意,你不知道需要多长时间,所以你肯定在正确的轨道上测试“加载”和“就绪”事件。您可能无法删除设置超时。由于打开的窗口是一个不同的DOM,因此没有可靠的方法来告诉您何时加载DOM内容,因此您将能够访问主体。如果删除超时,则会出现一半时间有效,另一半时间无效的情况,这取决于在尝试查询该DOM时新窗口是否已完成其DOM。我在我的项目中有完全相同的代码,没有找到一种方法可以在不使用超时和while循环的情况下100%工作。你不需要
media=“print”
如果你想一直隐藏它,它只会在打印时隐藏它。这确实非常适合静态页面,但对于单页面应用程序来说,它会造成混乱。如果你不小心,你可能会诱使应用程序改变状态。
<script>
  window.print();
  window.close();
</script>