選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

printElement.js 5.8 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /// <reference path="http://code.jquery.com/jquery-1.4.1-vsdoc.js" />
  2. /*
  3. * Print Element Plugin 1.2
  4. *
  5. * Copyright (c) 2010 Erik Zaadi
  6. *
  7. * Inspired by PrintArea (http://plugins.jquery.com/project/PrintArea) and
  8. * http://stackoverflow.com/questions/472951/how-do-i-print-an-iframe-from-javascript-in-safari-chrome
  9. *
  10. * Home Page : http://projects.erikzaadi/jQueryPlugins/jQuery.printElement
  11. * Issues (bug reporting) : http://github.com/erikzaadi/jQueryPlugins/issues/labels/printElement
  12. * jQuery plugin page : http://plugins.jquery.com/project/printElement
  13. *
  14. * Thanks to David B (http://github.com/ungenio) and icgJohn (http://www.blogger.com/profile/11881116857076484100)
  15. * For their great contributions!
  16. *
  17. * Dual licensed under the MIT and GPL licenses:
  18. * http://www.opensource.org/licenses/mit-license.php
  19. * http://www.gnu.org/licenses/gpl.html
  20. *
  21. * Note, Iframe Printing is not supported in Opera and Chrome 3.0, a popup window will be shown instead
  22. */
  23. ; (function (window, undefined) {
  24. var document = window["document"];
  25. var $ = window["jQuery"];
  26. $.fn["printElement"] = function (options) {
  27. var mainOptions = $.extend({}, $.fn["printElement"]["defaults"], options);
  28. //Remove previously printed iframe if exists
  29. $("[id^='printElement_']").remove();
  30. return this.each(function () {
  31. //Support Metadata Plug-in if available
  32. var opts = $.meta ? $.extend({}, mainOptions, $(this).data()) : mainOptions;
  33. _printElement($(this), opts);
  34. });
  35. };
  36. $.fn["printElement"]["defaults"] = {
  37. "printMode": 'iframe', //Usage : iframe / popup
  38. "pageTitle": '', //Print Page Title
  39. "overrideElementCSS": null,
  40. /* Can be one of the following 3 options:
  41. * 1 : boolean (pass true for stripping all css linked)
  42. * 2 : array of $.fn.printElement.cssElement (s)
  43. * 3 : array of strings with paths to alternate css files (optimized for print)
  44. */
  45. "printBodyOptions": {
  46. "styleToAdd": 'padding:10px;margin:10px;', //style attributes to add to the body of print document
  47. "classNameToAdd": '' //css class to add to the body of print document
  48. },
  49. "leaveOpen": false, // in case of popup, leave the print page open or not
  50. "iframeElementOptions": {
  51. "styleToAdd": 'border:none;position:absolute;width:0px;height:0px;bottom:0px;left:0px;', //style attributes to add to the iframe element
  52. "classNameToAdd": '' //css class to add to the iframe element
  53. }
  54. };
  55. $.fn["printElement"]["cssElement"] = {
  56. "href": '',
  57. "media": ''
  58. };
  59. function _printElement(element, opts) {
  60. //Create markup to be printed
  61. var html = _getMarkup(element, opts);
  62. var popupOrIframe = null;
  63. var documentToWriteTo = null;
  64. if (opts["printMode"].toLowerCase() == 'popup') {
  65. popupOrIframe = window.open('about:blank', 'printElementWindow', 'width=650,height=440,scrollbars=yes');
  66. documentToWriteTo = popupOrIframe.document;
  67. }
  68. else {
  69. //The random ID is to overcome a safari bug http://www.cjboco.com.sharedcopy.com/post.cfm/442dc92cd1c0ca10a5c35210b8166882.html
  70. var printElementID = "printElement_" + (Math.round(Math.random() * 99999)).toString();
  71. //Native creation of the element is faster..
  72. var iframe = document.createElement('IFRAME');
  73. $(iframe).attr({
  74. style: opts["iframeElementOptions"]["styleToAdd"],
  75. id: printElementID,
  76. className: opts["iframeElementOptions"]["classNameToAdd"],
  77. frameBorder: 0,
  78. scrolling: 'no',
  79. src: 'about:blank'
  80. });
  81. document.body.appendChild(iframe);
  82. documentToWriteTo = (iframe.contentWindow || iframe.contentDocument);
  83. if (documentToWriteTo.document)
  84. documentToWriteTo = documentToWriteTo.document;
  85. iframe = document.frames ? document.frames[printElementID] : document.getElementById(printElementID);
  86. popupOrIframe = iframe.contentWindow || iframe;
  87. }
  88. focus();
  89. documentToWriteTo.open();
  90. documentToWriteTo.write(html);
  91. documentToWriteTo.close();
  92. _callPrint(popupOrIframe);
  93. };
  94. function _callPrint(element) {
  95. if (element && element["printPage"])
  96. element["printPage"]();
  97. else
  98. setTimeout(function () {
  99. _callPrint(element);
  100. }, 50);
  101. }
  102. function _getElementHTMLIncludingFormElements(element) {
  103. var $element = $(element);
  104. var elementHtml = $('<div></div>').append($element.clone()).html();
  105. return elementHtml;
  106. }
  107. function _getBaseHref() {
  108. var port = (window.location.port) ? ':' + window.location.port : '';
  109. return window.location.protocol + '//' + window.location.hostname + port + window.location.pathname;
  110. }
  111. function _getMarkup(element, opts) {
  112. var $element = $(element);
  113. var elementHtml = _getElementHTMLIncludingFormElements(element);
  114. var html = new Array();
  115. html.push('<html><head><title>' + opts["pageTitle"] + '</title>');
  116. //Ensure that relative links work
  117. html.push('<base href="' + _getBaseHref() + '" />');
  118. html.push('</head><body style="' + opts["printBodyOptions"]["styleToAdd"] + '" class="' + opts["printBodyOptions"]["classNameToAdd"] + '">');
  119. html.push('<div class="' + $element.attr('class') + '">' + elementHtml + '</div>');
  120. html.push('<script type="text/javascript">function printPage(){focus();print();'
  121. + ((!opts["leaveOpen"] && opts["printMode"].toLowerCase() == 'popup') ? 'close();' : '') + '}</script>');
  122. html.push('</body></html>');
  123. return html.join('');
  124. };
  125. })(window);