javascript - 打印预览关闭后未触发 OnPrintDialogClose 方法

标签 javascript html printjs

嗨,我正在使用 PrintJs javascript 库。我在 OnPrintDialogClose 选项中分配了一个回调函数。它不会在打印预览窗口关闭后立即触发,而是仅在我切换到新的浏览器选项卡或在 Windows 操作系统上按 Alt+Tab 后触发。有谁知道为什么打印预览窗口关闭后没有触发。

这是我的Codepen sample

这是我的 JavaScript 代码片段。

<script>
    var afterPreviewClose = function(){ 
       window.location = "index.html"; 
    };
    $("#btnPrint").on("click",function(){ 
       printJS({ 
          printable: 'print-area',
          type: 'html', 
          onPrintDialogClose: afterPreviewClose,
          css: 'styles/styles.css' }); 
       }); 
</script>

最佳答案

我在以下链接中尝试了 slebetman 的解决方案 https://github.com/crabbly/Print.js/issues/348

let focuser = setInterval(() => window.dispatchEvent(new Event('focus')), 500);

printJs({

    printable: url,

    onPrintDialogClose: () => {

    clearInterval(focuser);

    // do your thing..

},

onError: () => {

    clearInterval(focuser);

    // do your thing..

  }

});

关于javascript - 打印预览关闭后未触发 OnPrintDialogClose 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63446345/

相关文章:

javascript - 如何用 Cypress stub contentwindow.print/用 Cypress 测试 printJS

javascript - 使用 webpack 导入 print.js

javascript - REGEX:删除以下日期模式末尾的逗号

javascript - jQuery .click() 事件停止工作

javascript - KnockoutJs 更新计数器后过滤器

javascript - 无法在 javafx Webview 上加载 CSS/JS

html - 响应式设计中 TD 垂直对齐的最大边距

javascript - 是否有标准函数来检查 JavaScript 中的 null、未定义或空白变量?

php - 无法使 PHP 更新功能正常工作

javascript - printJS - 为什么打印 PDF 文件时请求的 URL 未找到错误?