jquery.PrintArea.js 1010 B

12345678910111213141516171819202122232425262728293031323334353637
  1. // JavaScript Document
  2. (function($) {
  3. var printAreaCount = 0;
  4. $.fn.printArea = function()
  5. {
  6. var ele = $(this);
  7. var idPrefix = "printArea_";
  8. removePrintArea( idPrefix + printAreaCount );
  9. printAreaCount++;
  10. var iframeId = idPrefix + printAreaCount;
  11. var iframeStyle = 'position:absolute;width:0px;height:0px;left:-500px;top:-500px;';
  12. iframe = document.createElement('IFRAME');
  13. $(iframe).attr({ style : iframeStyle,
  14. id : iframeId
  15. });
  16. document.body.appendChild(iframe);
  17. var doc = iframe.contentWindow.document;
  18. $(document).find("link")
  19. .filter(function(){
  20. return $(this).attr("rel").toLowerCase() == "stylesheet";
  21. })
  22. .each(function(){
  23. doc.write('<link type="text/css" rel="stylesheet" href="' +
  24. $(this).attr("href") + '" >');
  25. });
  26. doc.write('<div class="' + $(ele).attr("class") + '">' + $(ele).html() + '</div>');
  27. doc.close();
  28. var frameWindow = iframe.contentWindow;
  29. frameWindow.close();
  30. frameWindow.focus();
  31. frameWindow.print();
  32. }
  33. var removePrintArea = function(id)
  34. {
  35. $( "iframe#" + id ).remove();
  36. };
  37. })(jQuery);