jquery.jqprint-0.3.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // -----------------------------------------------------------------------
  2. // Eros Fratini - eros@recoding.it
  3. // jqprint 0.3
  4. //
  5. // - 19/06/2009 - some new implementations, added Opera support
  6. // - 11/05/2009 - first sketch
  7. //
  8. // Printing plug-in for jQuery, evolution of jPrintArea: http://plugins.jquery.com/project/jPrintArea
  9. // requires jQuery 1.3.x
  10. //
  11. // Licensed under the MIT license: http://www.opensource.org/licenses/mit-license.php
  12. //------------------------------------------------------------------------
  13. (function($) {
  14. var opt;
  15. $.fn.jqprint = function (options) {
  16. opt = $.extend({}, $.fn.jqprint.defaults, options);
  17. var $element = (this instanceof jQuery) ? this : $(this);
  18. if (opt.operaSupport && $.browser.opera)
  19. {
  20. var tab = window.open("","jqPrint-preview");
  21. tab.document.open();
  22. var doc = tab.document;
  23. }
  24. else
  25. {
  26. var $iframe = $("<iframe />");
  27. if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }
  28. $iframe.appendTo("body");
  29. var doc = $iframe[0].contentWindow.document;
  30. }
  31. if (opt.importCSS)
  32. {
  33. doc.write('<style media="print"> @page {size: auto; margin: 0mm;padding:0mm }</style>')
  34. if ($("link[media=print]").length > 0)
  35. {
  36. $("link[media=print]").each( function() {
  37. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
  38. });
  39. }
  40. else
  41. {
  42. $("link").each( function() {
  43. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
  44. });
  45. }
  46. }
  47. if (opt.printContainer) { doc.write($element.outer()); }
  48. else { $element.each( function() { doc.write($(this).html()); }); }
  49. doc.close();
  50. (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
  51. setTimeout( function() { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);
  52. }
  53. $.fn.jqprint.defaults = {
  54. debug: false,
  55. importCSS: true,
  56. printContainer: true,
  57. operaSupport: true
  58. };
  59. // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
  60. jQuery.fn.outer = function() {
  61. return $($('<div></div>').html(this.clone())).html();
  62. }
  63. })(jQuery);