jquery.jqprint-0.3.js 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. if ($("link[media=print]").length > 0)
  34. {
  35. $("link[media=print]").each( function() {
  36. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
  37. });
  38. }
  39. else
  40. {
  41. $("link").each( function() {
  42. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
  43. });
  44. }
  45. }
  46. if (opt.printContainer) { doc.write($element.outer()); }
  47. else { $element.each( function() { doc.write($(this).html()); }); }
  48. doc.close();
  49. (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
  50. setTimeout( function() {
  51. (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print();
  52. if (tab) {
  53. tab.close();
  54. }
  55. },1000);
  56. }
  57. $.fn.jqprint.defaults = {
  58. debug: false,
  59. importCSS: true,
  60. printContainer: true,
  61. operaSupport: true
  62. };
  63. // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
  64. jQuery.fn.outer = function() {
  65. return $($('<div></div>').html(this.clone())).html();
  66. }
  67. })(jQuery);