jquery.jqprint-0.3.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. (function ($) {
  2. var opt; $.fn.jqprint = function (options) {
  3. opt = $.extend({
  4. }, $.fn.jqprint.defaults, options); var $element = (this instanceof jQuery) ? this : $(this); if (opt.operaSupport && $.browser.opera) {
  5. var tab = window.open("", "jqPrint-preview"); tab.document.open(); var doc = tab.document;
  6. }
  7. else {
  8. var $iframe = $("<iframe />"); if (!opt.debug) {
  9. $iframe.css({
  10. position: "absolute", width: "0px", height: "0px", left: "-600px", top: "-600px"
  11. });
  12. }
  13. $iframe.appendTo("body"); var doc = $iframe[0].contentWindow.document;
  14. }
  15. if (opt.importCSS) {
  16. if ($("link[media=print]").length > 0) {
  17. $("link[media=print]").each(function () {
  18. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />");
  19. });
  20. if (opt.portrait) {
  21. doc.write('<style> @page { size: landscape;}</style>');
  22. } else {
  23. doc.write('<style> @page { size: portrait;}</style>');
  24. }
  25. }
  26. else {
  27. $("link").each(function () {
  28. doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />");
  29. });
  30. if (opt.portrait) {
  31. doc.write('<style> @page { size: landscape;}</style>');
  32. } else {
  33. doc.write('<style> @page { size: portrait;}</style>');
  34. }
  35. }
  36. }
  37. if (opt.printContainer) {
  38. doc.write($element.outer());
  39. }
  40. else {
  41. $element.each(function () {
  42. doc.write($(this).html());
  43. });
  44. }
  45. doc.close(); (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus(); setTimeout(function () {
  46. (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print();
  47. if (tab) {
  48. tab.close();
  49. }
  50. }, 1000);
  51. }
  52. $.fn.jqprint.defaults = {
  53. debug: false, importCSS: true, printContainer: true, operaSupport: true, portrait: true
  54. };
  55. jQuery.fn.outer = function () {
  56. return $($('<div></div>').html(this.clone())).html();
  57. }
  58. })(jQuery);