jquery.hiwprint.js 3.7 KB

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