window.print not working in Firefox

强颜欢笑 提交于 2019-12-18 08:40:50

问题


function CallPrint() {
        var prtContent = document.getElementById('<%= pnlDelete.ClientID %>');
        var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
        winPrint.document.write("<h3>Summary</h3><br />" + prtContent.innerHTML);
        winPrint.document.close();
        winPrint.focus();
        winPrint.print();
        winPrint.close();
    }

I have a need where I have to print contents of a div. I am using above code to do so. It is working fine in IE but does nothing in Firefox. Am I missing something here that needs to be done in Firefox?


回答1:


Instead of opening a new window without any URL, I opened this page in the window and accessed the contents of the pnlSummary from the opened window via window.opener object –

function CallPrint() {
    var winPrint = window.open('Print.aspx', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
}

On Print.aspx page I used this function –

function Print() {
    var prtContent = "<h3>Summary</h3>" + window.opener.document.getElementById('ctl00_cphContent_pnlSummary').innerHTML;
    document.getElementById("printDiv").innerHTML = prtContent;
    window.print();
    window.opener.focus();
    window.close(); }

and called it on body onload.

<body onload="Print();">
    <form id="form1" runat="server">
    <div id="printDiv">
    </div>
    </form>
</body>

This is working fine in both IE and Firefox.




回答2:


Use setTimeout() function for loading the page. The example is given bellow link.

http://oraclehappy2help.blogspot.in/2012/09/child-window-printing-problem-solution.html




回答3:


Uhm... your code seems to work fine for me, on Firefox 3.5 (Windows). It's possible that are something wrong on your pnlDelete.ClientID? Your javascript code is rendered well on the page?

Anyway I suggest you to use jQuery + a print plugin like this.




回答4:


Check to ensure your panel has something. My guess is prtContent is undefined

Try this:

function CallPrint() {
    var prtContent = document.getElementById('<%= pnlDelete.ClientID %>');

    if (prtContent) {
        var winPrint = window.open('', '', 'left=0,top=0,width=800,height=600,toolbar=0,scrollbars=0,status=0');
        winPrint.document.write("<h3>Summary</h3><br />" + prtContent.innerHTML);
        winPrint.document.close();
        winPrint.focus();
        winPrint.print();
        winPrint.close();
    }
    else {
        alert('No summary available for printing');
    }
}



回答5:


you can use JS Printer Setup https://addons.mozilla.org/en-us/firefox/addon/js-print-setup/"

which is Fire fox Depended addon most usefulladdon In web-app Kisok in Firefox to select printer

attached some example for attached printer and local printer it may help you to build without print dialog.

function EB_Print(printType) {
	try{
		var printerType = printType; // type of the Print Code : network 
		// Default Printer Configuring
		var Default_printer = "Canon MG2500 series";
		
		/** local Printer configuring via Network
		 ** Config teh Local server use \\\\ to get \\ 
		 **/
		var Organizer_Printer = "\\\\network\\Canon LBP2900";
		
		jsPrintSetup.setPrinter(Default_printer);
		jsPrintSetup.setSilentPrint(true);// withoud dialog 
		
		/** alert(jsPrintSetup.getPrintersList()); // Debugger for the attached Printers list
		 	alert(jsPrintSetup.getPrinter());   // get the set printer Option
		**/
	// id network is selected It will print the page in network
		if(printerType == 'network'){
			jsPrintSetup.setPrinter(Organizer_Printer);
		}
		 jsPrintSetup.print(); // Print the page
		 
		
	}catch (e) {
		// TODO: handle exception
	}
	
}



回答6:


you could try a jquery plugin...

http://plugins.jquery.com/project/PrintArea



来源:https://stackoverflow.com/questions/1999108/window-print-not-working-in-firefox

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!