最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Is it possible to cancel print in javascript - Stack Overflow

programmeradmin6浏览0评论

I am using the code found here: Detecting browser print event to detect if the user wants to print the page. So far it works as intended.

In the "afterPrint"-function I call a function which creates and appends an iframe containing another page with a different, and much more printfriendly layout of what needs to be printed:

    function printIframe (printsrc) {
        var iframe = $('iframe#print');
        if (iframe.length == 0)
            iframe = $('<iframe id="print" />').attr('src', printsrc).css("width", "0px").css("height", "0px").css("position", "absolute").css("left", "-9999px").appendTo($("body:first"));
        else
            iframe.attr('src', printsrc);
    };

On the more printerfriendly page I create my layout and then call: window.print();

By creating and appending an iframe, the user stays on the page in the browser, but the print layout is different.

It seems the only way to get the printerfriendly page to load is by cancelling the print in the print dialog, which then triggers the afterPrint-function. Is it somehow possible to cancel the user's print dialog in javascript ?

thanks,

Thomas

I am using the code found here: Detecting browser print event to detect if the user wants to print the page. So far it works as intended.

In the "afterPrint"-function I call a function which creates and appends an iframe containing another page with a different, and much more printfriendly layout of what needs to be printed:

    function printIframe (printsrc) {
        var iframe = $('iframe#print');
        if (iframe.length == 0)
            iframe = $('<iframe id="print" />').attr('src', printsrc).css("width", "0px").css("height", "0px").css("position", "absolute").css("left", "-9999px").appendTo($("body:first"));
        else
            iframe.attr('src', printsrc);
    };

On the more printerfriendly page I create my layout and then call: window.print();

By creating and appending an iframe, the user stays on the page in the browser, but the print layout is different.

It seems the only way to get the printerfriendly page to load is by cancelling the print in the print dialog, which then triggers the afterPrint-function. Is it somehow possible to cancel the user's print dialog in javascript ?

thanks,

Thomas

Share Improve this question edited May 23, 2017 at 11:56 CommunityBot 11 silver badge asked Mar 9, 2015 at 8:06 ThomasDThomasD 2,4947 gold badges40 silver badges58 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 8

No it is not possible to cancle the print event using Javascript.

Read this: [MDN, beforeprint - Cancelable:NO]

Also see the fiddle:

var beforePrint = function() {
        console.log('Functionality to run before printing.');
        alert('cancelable : '+event.cancelable)
    };
发布评论

评论列表(0)

  1. 暂无评论