//提交打印 function sbumitPrint() { printHidden("AppsDSPrintDoub.aspx?type=print"); }
function printFrame(frame, onfinish) { if (!frame) frame = window; function execOnFinish() { switch (typeof (onfinish)) { case "string": execScript(onfinish); break; case "function": onfinish(); break; } if (focused && !focused.disabled) focused.focus(); } if (frame.document.readyState !== "complete") { execOnFinish(); return; } // var eventScope = printGetEventScope(frame); // var focused = document.activeElement; window.printHelper = function() { // printWB.ExecWB(6, 2); // printFireEvent(frame, eventScope, "onafterprint"); // printWB.outerHTML = ""; // execOnFinish(); //frame.jqprint(); var prints = $(frame.document.getElementById('printTable')); //打印table的ID prints.jqprint(); window.printHelper = null; } //document.body.insertAdjacentHTML("beforeEnd", "<object id="printWB" width=0 height=0 classid="clsid:8856F961-340A-11D0-A96B-00C04FD705A2">"); //printFireEvent(frame, eventScope, "onbeforeprint"); frame.focus(); window.printHelper(); } function printIsNativeSupport() { var agent = window.navigator.userAgent; var i = agent.indexOf("MSIE ") + 5; return parseInt(agent.substr(i)) >= 5 && agent.indexOf("5.0b1") < 0; } function printFireEvent(frame, obj, name) { var handler = obj[name]; switch (typeof (handler)) { case "string": frame.execScript(handler); break; case "function": handler(); break; } } function printGetEventScope(frame) { var frameset = frame.document.all.tags("FRAMESET"); if (frameset.length) return frameset[0]; return frame.document.body; } function printHidden(url) { document.body.insertAdjacentHTML("beforeEnd", "<iframe name='printHiddenFrame' width='0' height='0'></iframe>"); var doc = printHiddenFrame.document; doc.open(); doc.write("<body onload="parent.onprintHiddenFrame()">"); doc.write("<iframe name='printMe' width='0' height='0' src="" + url + ""></iframe>"); doc.write("</body>"); doc.close(); } function onprintHiddenFrame() { function onfinish() { printHiddenFrame.outerHTML = ""; if (window.onprintcomplete) window.onprintcomplete(); } printFrame(printHiddenFrame.printMe, onfinish); }
这里还要用到一个jqprint-0.3.js
// ----------------------------------------------------------------------- // Eros Fratini - eros@recoding.it // jqprint 0.3 //------------------------------------------------------------------------ (function($) { var opt; $.fn.jqprint = function (options) { opt = $.extend({}, $.fn.jqprint.defaults, options); var $element = (this instanceof jQuery) ? this : $(this); if (opt.operaSupport && $.browser.opera) { var tab = window.open("","jqPrint-preview"); tab.document.open(); var doc = tab.document; } else { var $iframe = $("<iframe />"); if (!opt.debug) { $iframe.css({ position: "absolute", "0px", height: "0px", left: "-600px", top: "-600px" }); } $iframe.appendTo("body"); var doc = $iframe[0].contentWindow.document; } if (opt.importCSS) { if ($("link[media=print]").length > 0) { $("link[media=print]").each( function() { doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' media='print' />"); }); } else { $("link").each( function() { doc.write("<link type='text/css' rel='stylesheet' href='" + $(this).attr("href") + "' />"); }); } } if (opt.printContainer) { doc.write($element.outer()); } else { $element.each( function() { doc.write($(this).html()); }); } doc.close(); (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).focus(); setTimeout( function() { (opt.operaSupport && $.browser.opera ? tab : $iframe[0].contentWindow).print(); if (tab) { tab.close(); } }, 1000); } $.fn.jqprint.defaults = { debug: false, importCSS: true, printContainer: true, operaSupport: true }; // Thanks to 9__, found at http://users.livejournal.com/9__/380664.html jQuery.fn.outer = function() { return $($('<div></div>').html(this.clone())).html(); } })(jQuery);
可以直接调用jqprint-0.3.js,这样做是为了在提交数据的时候直接打印,等于后台打印,就不要重新打开一个预览页面