You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

69 lines
2.4 KiB

2 years ago
  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. var tab = window.open("", "jqPrint-preview");
  20. tab.document.open();
  21. var doc = tab.document;
  22. }
  23. else {
  24. var $iframe = $("<iframe />");
  25. if (!opt.debug) { $iframe.css({ position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px" }); }
  26. $iframe.appendTo("body");
  27. var doc = $iframe[0].contentWindow.document;
  28. }
  29. if (opt.importCSS) {
  30. if ($("link[media=print]").length > 0) {
  31. $("link[media=print]").each(function () {
  32. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
  33. });
  34. }
  35. else {
  36. $("link").each(function () {
  37. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
  38. });
  39. }
  40. }
  41. if (opt.printContainer) { doc.write($element.outer()); }
  42. else { $element.each(function () { doc.write($(this).html()); }); }
  43. doc.close();
  44. (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus();
  45. setTimeout(function () { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000);
  46. }
  47. $.fn.jqprint.defaults = {
  48. debug: false,
  49. importCSS: true,
  50. printContainer: true,
  51. operaSupport: true
  52. };
  53. // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html
  54. jQuery.fn.outer = function () {
  55. return $($('<div></div>').html(this.clone())).html();
  56. }
  57. })(jQuery);