iOS 8 Safari打印重定向不会停止javascript执行 [英] iOS 8 Safari print redirect doesn't stop javascript execution

查看:76
本文介绍了iOS 8 Safari打印重定向不会停止javascript执行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要提供在成功保存时以及在打印重定向到搜索页面之后打印标签的功能.这在chrome,firefox(即iOS 6/7野生动物园)等环境中有效.但是,当从javascript发出window.print()时,iOS 8似乎不再停止执行javascript.

I have the need to provide the ability to print a label on successful save and after the print redirect to a search page. This works in chrome, firefox, ie, iOS 6/7 safari, etc. However, it seems that iOS 8 no longer stops the execution of javascript when window.print() is issued from javascript.

如果您从iOS 8 Safari浏览到此jsfiddle示例(已连接到计算机,因此您可以查看控制台日志),然后单击打印"按钮,您会看到当打印对话框打开时console.log将触发.因此,如果您要打印然后导航,将打印错误的屏幕,除非您有延迟,使您有足够的时间进行打印,这在这种情况下是不可接受的.

If you navigate to this jsfiddle example from iOS 8 Safari (connected to a computer so you can view the console logs) and click the Print button you will see that the console.log will trigger when the print dialog is up. So if you want to print and then navigate you will print the wrong screen unless you have a delay that gives you enough time to hit print which isn't acceptable in this case.

我做了人为的延迟,因为在iOS 6/7中,它似乎获得了打印对话框,最终停止了javascript的执行.在这种情况下,500毫秒足以使其正常工作.

I did an artificial delay because in iOS 6/7 that seemed to get the print dialog to eventually stop the execution of javascript. In that case 500ms was enough to make it work.

在其他人通过Safari在iOS 8中执行类似操作时,是否有其他人看到过此问题?他们是否引入了一个新事件来监听我可以用来做的事?

Has anyone else seen this issue when doing a similar thing in iOS 8 from Safari? Have they introduced a new event to listen for that I could use to do this?

// Example Code
window.print();
setTimeout(function() {
    console.log('This should print after the print is issued in the iOS print dialog.');
}, 500);

推荐答案

您可以使用window.matchMedia( caniuse链接),与window.onbeforeprintwindow.onafterprint结合使用(用于早期的IE支持).在此处此处.

You can use window.matchMedia (caniuse link), combined with window.onbeforeprint and window.onafterprint (for earlier IE support). A good reference for using matchMedia can be found here and here.

要满足在iOS上使用matchMedia的要求,请使用嵌套事件.首先,侦听媒体类型以更改为print.然后,在该回调中,侦听媒体更改回screen.

To satisfy using matchMedia with iOS, use nested events. First, listen for the media type to change to print. Then, inside that callback, listen for the media to change back to screen.

if (window.matchMedia) {
    var printQuery = window.matchMedia('print');
    printQuery.addListener(function() {
        var screenQuery = window.matchMedia('screen');
        screenQuery.addListener(function() {
            //actions after print dialog close here
        });
    });
}

这篇关于iOS 8 Safari打印重定向不会停止javascript执行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆