Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/439.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
Safari,javascript:等待打印确认窗口_Javascript_Safari - Fatal编程技术网

Safari,javascript:等待打印确认窗口

Safari,javascript:等待打印确认窗口,javascript,safari,Javascript,Safari,我有这样的代码: hideStuff(); window.print(); showStuff(); function safariPrint() { // Safari 12, macOS if (!window.onafterprint) { const onAfterPrint = mql => { if (!mql.matches) { showStuff(); // printing is finished =>

我有这样的代码:

hideStuff();
window.print();
showStuff();
function safariPrint() {
  // Safari 12, macOS
  if (!window.onafterprint) {
    const onAfterPrint = mql => {
      if (!mql.matches) {
        showStuff();

        // printing is finished => unsubscribe to avoid leaks
        if (mediaQueryList.removeEventListener) {
          mediaQueryList.removeEventListener('change', onAfterPrint);
        } else {
          mediaQueryList.removeListener(onAfterPrint);
        }
      }
      window.onfocus = null;
    };

    // a tiny polyfill for a plain onafterprint
    // to handle standard window.print() dialog events
    const mediaQueryList = window.matchMedia('print');
    if (mediaQueryList.addEventListener) {
      mediaQueryList.addEventListener('change', onAfterPrint);
    } else {
      mediaQueryList.addListener(onAfterPrint);
    }

    // if a user cancels printing in Safari's print confirmation dialog
    // then we will trigger a cleanup
    window.focus();
    window.onfocus = () => {
      onAfterPrint(mediaQueryList);
    };
  }

  hideStuff();    
  window.print();
}
因此,
hideStuff()
会隐藏页面上的某些元素,以便它们不会打印,而
showStuff()
会在打印对话框关闭后恢复这些隐藏的元素

这在我第一次单击打印按钮时在Safari上起作用,但是如果我随后取消打印对话框,返回页面并再次单击打印按钮,Safari会弹出一条消息,内容为“此网页正在尝试打印。是否要打印此网页?”如果我继续,生成的打印预览包含页面的所有元素,甚至是那些应该隐藏的元素

问题似乎是“确定”对话框延迟打开
窗口。print()
,但它确实允许javascript继续。也就是说,
showStuff()
会立即运行,而不是在打印对话框关闭后运行

当“您确定”框打开时,如何停止执行


谢谢

我遇到了同样的问题,我的调查显示,每当用户点击取消(或键盘上的Esc按钮)关闭确认对话框时,
onfocus
事件就会触发。 因此,基于
onfocus
事件的解决方案可能如下所示:

hideStuff();
window.print();
showStuff();
function safariPrint() {
  // Safari 12, macOS
  if (!window.onafterprint) {
    const onAfterPrint = mql => {
      if (!mql.matches) {
        showStuff();

        // printing is finished => unsubscribe to avoid leaks
        if (mediaQueryList.removeEventListener) {
          mediaQueryList.removeEventListener('change', onAfterPrint);
        } else {
          mediaQueryList.removeListener(onAfterPrint);
        }
      }
      window.onfocus = null;
    };

    // a tiny polyfill for a plain onafterprint
    // to handle standard window.print() dialog events
    const mediaQueryList = window.matchMedia('print');
    if (mediaQueryList.addEventListener) {
      mediaQueryList.addEventListener('change', onAfterPrint);
    } else {
      mediaQueryList.addListener(onAfterPrint);
    }

    // if a user cancels printing in Safari's print confirmation dialog
    // then we will trigger a cleanup
    window.focus();
    window.onfocus = () => {
      onAfterPrint(mediaQueryList);
    };
  }

  hideStuff();    
  window.print();
}

CSS印刷媒体。你为什么不用它?设置打印样式表,以隐藏您不希望看到的内容。我正在使用打印媒体查询。为了说明问题,对上述内容进行了简化。前后函数存在的原因远远超出了这个特定问题的范围。具有完全相同的问题。。。只在狩猎中发生。欢迎找到解决办法!可能重复的