wxw
5 天以前 af583e3044e03325d8fdbc08164e8de3a0021792
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
(function ($) {
    $.fn.hiwprint = function (options) {
        var usedFrame = document.getElementById('hiwprint_iframe');
        if (usedFrame) usedFrame.parentNode.removeChild(usedFrame);
        var opt = $.extend({}, $.fn.hiwprint.defaults, options);
        var $element = this;
        var $iframe = $('<iframe id="hiwprint_iframe"  style="visibility: hidden; height: 0; width: 0; position: absolute;"></iframe>');
        var css = '';
        if (opt.importCss) {
            if ($("link[media=print]").length > 0) {
                $("link[media=print]").each(function () {
                    css += '<link rel="stylesheet" type="text/css" media="print" href="' + $(this).attr("href") + '">';
                });
            }
            else {
                $("link").each(function () {
                    css += '<link rel="stylesheet" type="text/css" media="print" href="' + $(this).attr("href") + '">';
                });
            }
        }
        $iframe[0].srcdoc = '<!DOCTYPE html><html><head><title></title><meta charset="UTF-8">' + css + '</head><body></body></html>';
 
        $iframe[0].onload = function () {
            var printDocument = $iframe[0].contentWindow || $iframe[0].contentDocument;
            if (printDocument.document) printDocument = printDocument.document;
            if (!$iframe.attr('srcdoc')) {
                printDocument.write('<!DOCTYPE html><html><head><title></title><meta charset="UTF-8">' + css + '</head><body></body></html>');
            }
            if (opt.printContainer) {
                printDocument.body.innerHTML = $element[0].outerHTML;
            }
            else {
                printDocument.body.innerHTML = $element.html();
            }
            loadAllImages(printDocument, function () {
 
                performPrint($iframe[0]);
            });
 
        };
 
        $iframe.appendTo("body");
 
    };
 
    $.fn.hiwprint.defaults = {
        importCss: true,
        printContainer: true
    };
 
    function performPrint(iframeElement) {
        try {
            iframeElement.focus();
            if (isEdge() || isIE()) {
                try {
                    iframeElement.contentWindow.document.execCommand('print', false, null);
                } catch (e) {
                    iframeElement.contentWindow.print();
                }
            } else {
                // Other browsers
                iframeElement.contentWindow.print();
            }
        } catch (error) {
            console.log(error);
        }
    }
 
 
    function isIE() {
        return navigator.userAgent.indexOf('MSIE') !== -1 || !!document.documentMode;
    }
 
    // Edge 20+
    function isEdge() {
        return !isIE() && !!window.StyleMedia;
    }
 
 
 
    function loadAllImages(printDocument, callback, time) {
        
        if (time === undefined) {
            time = 0;
        }
        var images = printDocument.getElementsByTagName('img');
        var allLoaded = true;
        for (var i = 0; i < images.length; i++) {
            var image = images[i];
            if (image.src && image.src !== window.location.href && image.src.indexOf('base64') == -1) {
               
                if (!image || typeof image.naturalWidth === 'undefined' || image.naturalWidth === 0 || !image.complete) {
                    console.log(image.complete);
                    if (!image.complete) {
                        allLoaded = false;
                    }
                    
                }
            }
        }
        time++;
        if (!allLoaded && time < 10) {
            
            setTimeout(function () {
               loadAllImages(printDocument, callback, time);
            }, 500);
        } else {
            callback();
        }
    }
 
 
})(jQuery);