/** * version 2.4.0 copyright (c) 2013 * tested in ie 11, ff 28.0 and chrome 33.0.1750.154 * no official support for other browsers, but will try to accommodate challenges in other browsers. * example: * print button: * print area :
... html ...
* javascript : * options are passed as json (example: {mode: "popup", popclose: false}) * * {options} | [type] | (default), values | explanation * --------- | --------- | ---------------------- | ----------- * @mode | [string] | (iframe),popup | printable window is either iframe or browser popup * @popht | [number] | (500) | popup window height * @popwd | [number] | (400) | popup window width * @popx | [number] | (500) | popup window screen x position * @popy | [number] | (500) | popup window screen y position * @poptitle | [string] | ('') | popup window title element * @popclose | [boolean] | (false),true | popup window close after printing * @extracss | [string] | ('') | comma separated list of extra css to include * @retainattr | [string[]] | ["id","class","style"] | string array of attributes to retain for the containment area. (ie: id, style, class) * @standard | [string] | strict, loose, (html5) | only for popup. for html 4.01, strict or loose document standard, or html 5 standard * @extrahead | [string] | ('') | comma separated list of extra elements to be appended to the head tag */ (function ($) { var counter = 0; var modes = { iframe: "iframe", popup: "popup" }; var standards = { strict: "strict", loose: "loose", html5: "html5" }; var defaults = { mode: modes.iframe, standard: standards.html5, popht: 500, popwd: 400, popx: 200, popy: 200, poptitle: '', popclose: false, extracss: '', extrahead: '', retainattr: ["id", "class", "style"] }; var settings = {}; //global settings $.fn.printarea = function (options) { $.extend(settings, defaults, options); counter++; var idprefix = "printarea_"; $("[id^=" + idprefix + "]").remove(); settings.id = idprefix + counter; var $printsource = $(this); var printareawindow = printarea.getprintwindow(); printarea.write(printareawindow.doc, $printsource); settimeout(function () { printarea.print(printareawindow); }, 1000); }; var printarea = { print: function (pawindow) { var pawindow = pawindow.win; $(pawindow.doc).ready(function () { pawindow.focus(); pawindow.print(); if (settings.mode == modes.popup && settings.popclose) { settimeout(function () { pawindow.close(); }, 2000); } }); }, write: function (padocument, $ele) { padocument.open(); padocument.write(printarea.doctype() + "" + printarea.gethead() + printarea.getbody($ele) + ""); padocument.close(); }, doctype: function () { if (settings.mode == modes.iframe) { return ""; } if (settings.standard == standards.html5) { return ""; } var transitional = settings.standard == standards.loose ? " transitional" : ""; var dtd = settings.standard == standards.loose ? "loose" : "strict"; return ''; }, gethead: function () { var extrahead = ""; var links = ""; if (settings.extrahead) { settings.extrahead.replace(/([^,]+)/g, function (m) { extrahead += m }); } $(document).find("link") .filter(function () { // requirement: element must have rel="stylesheet" to be considered in print document var relattr = $(this).attr("rel"); return ($.type(relattr) === 'undefined') == false && relattr.tolowercase() == 'stylesheet'; }) .filter(function () { // include if media is undefined, empty, print or all var mediaattr = $(this).attr("media"); return $.type(mediaattr) === 'undefined' || mediaattr == "" || mediaattr.tolowercase() == 'print' || mediaattr.tolowercase() == 'all'; }) .each(function () { links += ''; }); if (settings.extracss) { settings.extracss.replace(/([^,\s]+)/g, function (m) { links += '' }); } return "" + settings.poptitle + "" + extrahead + links + ""; }, getbody: function (elements) { var htm = ""; var attrs = settings.retainattr; elements.each(function () { var ele = printarea.getformdata($(this)); var attributes = ""; for (var x = 0; x < attrs.length; x++) { var eleattr = $(ele).attr(attrs[x]); if (eleattr) { attributes += (attributes.length > 0 ? " " : "") + attrs[x] + "='" + eleattr + "'"; } } htm += '
' + $(ele).html() + '
'; }); return "" + htm + ""; }, getformdata: function (ele) { var copy = ele.clone(); var copiedinputs = $("input,select,textarea", copy); $("input,select,textarea", ele).each(function (i) { var typeinput = $(this).attr("type"); if ($.type(typeinput) === 'undefined') { typeinput = $(this).is("select") ? "select" : $(this).is("textarea") ? "textarea" : ""; } var copiedinput = copiedinputs.eq(i); if (typeinput == "radio" || typeinput == "checkbox") { copiedinput.attr("checked", $(this).is(":checked")); } else if (typeinput == "text") { copiedinput.attr("value", $(this).val()); } else if (typeinput == "select") { $(this).find("option").each(function (i) { if ($(this).is(":selected")) { $("option", copiedinput).eq(i).attr("selected", true); } }); } else if (typeinput == "textarea") { copiedinput.text($(this).val()); } }); return copy; }, getprintwindow: function () { switch (settings.mode) { case modes.iframe: var f = new printarea.iframe(); return { win: f.contentwindow || f, doc: f.doc }; case modes.popup: var p = new printarea.popup(); return { win: p, doc: p.doc }; } }, iframe: function () { var frameid = settings.id; var iframestyle = 'border:0;position:absolute;width:0px;height:0px;right:0px;top:0px;'; var iframe; try { iframe = document.createelement('iframe'); document.body.appendchild(iframe); $(iframe).attr({ style: iframestyle, id: frameid, src: "#" + new date().gettime() }); iframe.doc = null; iframe.doc = iframe.contentdocument ? iframe.contentdocument : (iframe.contentwindow ? iframe.contentwindow.document : iframe.document); } catch (e) { throw e + ". iframes may not be supported in this browser."; } if (iframe.doc == null) { throw "cannot find document."; } return iframe; }, popup: function () { var windowattr = "location=yes,statusbar=no,directories=no,menubar=no,titlebar=no,toolbar=no,dependent=no"; windowattr += ",width=" + settings.popwd + ",height=" + settings.popht; windowattr += ",resizable=yes,screenx=" + settings.popx + ",screeny=" + settings.popy + ",personalbar=no,scrollbars=yes"; var newwin = window.open("", "_blank", windowattr); newwin.doc = newwin.document; return newwin; } }; })(jquery);