jquery.jPrintArea.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. /**
  2. * Version 2.0
  3. * -Contributors: "mindinquiring" : filter to exclude any stylesheet other than print.
  4. * Tested ONLY in IE 8 and FF 3.5.3. No official support for other browsers, but will
  5. * TRY to accomodate challenges in other browsers.
  6. * Example:
  7. * Print Button: <div id="print_button">Print</div>
  8. * Print Area : <div class="PrintArea"> ... html ... </div>
  9. * Javascript : <script>
  10. * $("div#print_button").click(function(){
  11. * $("div.PrintArea").printArea( [OPTIONS] );
  12. * });
  13. * </script>
  14. * options are passed as json (json example: {mode: "popup", popClose: false})
  15. *
  16. * {OPTIONS} | [type] | (default), values | Explanation
  17. * --------- | --------- | -------------------- | -----------
  18. * @mode | [string] | ("iframe"), "popup" | printable window is either iframe or browser popup
  19. * @popHt | [number] | (500) | popup window height
  20. * @popWd | [number] | (400) | popup window width
  21. * @popX | [number] | (500) | popup window screen X position
  22. * @popY | [number] | (500) | popup window screen Y position
  23. * @popTitle | [string] | ('') | popup window title element
  24. * @popClose | [boolean] | (false), true | popup window close after printing
  25. */
  26. (function ($) {
  27. var counter = 0;
  28. var modes = {iframe: "iframe", popup: "popup"};
  29. var defaults = {
  30. mode: modes.iframe,
  31. popHt: 500,
  32. popWd: 400,
  33. popX: 200,
  34. popY: 200,
  35. popTitle: '',
  36. popClose: false
  37. };
  38. var settings = {};//global settings
  39. $.fn.printArea = function (options) {
  40. $.extend(settings, defaults, options);
  41. counter++;
  42. var idPrefix = "printArea_";
  43. $("[id^=" + idPrefix + "]").remove();
  44. var ele = $(this);
  45. settings.id = idPrefix + counter;
  46. var writeDoc;
  47. var printWindow;
  48. switch (settings.mode) {
  49. case modes.iframe :
  50. var f = new Iframe();
  51. writeDoc = f.doc;
  52. printWindow = f.contentWindow || f;
  53. break;
  54. case modes.popup :
  55. printWindow = new Popup();
  56. writeDoc = printWindow.doc;
  57. }
  58. writeDoc.open();
  59. writeDoc.write("<html>" + getHead() + getBody(ele) + "</html>");
  60. writeDoc.close();
  61. printWindow.focus();
  62. printWindow.print();
  63. if (settings.mode == modes.popup && settings.popClose)
  64. printWindow.close();
  65. }
  66. function getHead() {
  67. var head = "<head><title>" + settings.popTitle + "</title>";
  68. $(document).find("link")
  69. .filter(function () {
  70. return $(this).attr("rel").toLowerCase() == "stylesheet";
  71. })
  72. .filter(function () { // this filter contributed by "mindinquiring"
  73. var media = $(this).attr("media");
  74. return (media.toLowerCase() == "" || media.toLowerCase() == "print")
  75. })
  76. .each(function () {
  77. head += '<link type="text/css" rel="stylesheet" href="' + $(this).attr("href") + '" >';
  78. });
  79. head += "</head>";
  80. return head;
  81. }
  82. function getBody(printElement) {
  83. var body = "<body>";
  84. body += '<div class="' + $(printElement).attr("class") + '">' + $(printElement).html() + '</div>';
  85. body += "</body>";
  86. return body;
  87. }
  88. function Iframe() {
  89. var frameId = settings.id;
  90. var iframeStyle = 'border:0;position:absolute;width:0px;height:0px;left:0px;top:0px;';
  91. var iframe;
  92. //Downloads By http://www.veryhuo.com
  93. try {
  94. iframe = document.createElement('iframe');
  95. document.body.appendChild(iframe);
  96. $(iframe).attr({style: iframeStyle, id: frameId, src: ""});
  97. iframe.doc = null;
  98. iframe.doc = iframe.contentDocument ? iframe.contentDocument : (iframe.contentWindow ? iframe.contentWindow.document : iframe.document);
  99. } catch (e) {
  100. throw e + ". iframes may not be supported in this browser.";
  101. }
  102. if (iframe.doc == null) throw "Cannot find document.";
  103. return iframe;
  104. }
  105. function Popup() {
  106. var windowAttr = "location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no";
  107. windowAttr += ",width=" + settings.popWd + ",height=" + settings.popHt;
  108. windowAttr += ",resizable=yes,screenX=" + settings.popX + ",screenY=" + settings.popY + ",personalbar=no,scrollbars=no";
  109. var newWin = window.open("", "_blank", windowAttr);
  110. newWin.doc = newWin.document;
  111. return newWin;
  112. }
  113. })(jQuery);