Determine whether browser supports printing

前端 未结 3 1828
野性不改
野性不改 2020-12-03 09:15

I think the answer to this is almost certainly \"no\", because I\'ve done a little testing and searching around, but is there any trick to detect whether window.print(

相关标签:
3条回答
  • 2020-12-03 09:24

    Unfortunately it looks like a no. The window.print() function is not part of the EMCAScript specification. This means that there's no requirement for it to be part of the JavaScript language, and no proper documentation for its implementation. It's undefined behaviour and so testing for it looks very difficult.

    Sources:

    • https://developer.mozilla.org/en/DOM/window.print
    • http://www.ecma-international.org/publications/files/ECMA-ST/Ecma-262.pdf

    EDIT:

    Cute little script I wrote to test my browsers, just checks the print function exists and then asks to print:

    if(window.print) {
        if(confirm('I can print. Would you like to?'))
            window.print()
    }
    
    0 讨论(0)
  • 2020-12-03 09:26

    The print() method is synchronous. This makes it possible to do the aftermath in order to decide wether a print dialog has been shown

    var start = +new Date();
    window.print();
    var delta = + new Date() - start;
    console.log(delta);
    if (delta > 100) { console.log('It worked'); }
    
    0 讨论(0)
  • 2020-12-03 09:39

    The beforeprint and afterprint events may help, but I'm not sure about browser support.

    Edit: Webkit does not support them

    0 讨论(0)
提交回复
热议问题