web 后台打印
1 2 3 4 | //提交打印 function sbumitPrint() { printHidden( "AppsDSPrintDoub.aspx?type=print" ); } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 | 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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 | // ----------------------------------------------------------------------- // 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" , width: "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,这样做是为了在提交数据的时候直接打印,等于后台打印,就不要重新打开一个预览页面
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步