www.webdeveloper.com
Results 1 to 11 of 11

Thread: Printing from within popup window - doesn't work in IE

  1. #1
    Join Date
    Sep 2009
    Posts
    6

    Printing from within popup window - doesn't work in IE

    Hi,
    I create, open and populate a popup window:

    Code:
    function DisplayPopup (aObject) {
    ...
    var win2; //popup window
    
      win2 = window.open('', 'Description', 'alwaysRaised=yes,width=300,height=400); 
      win2.document.write('<H2>' + Item + ' Description</H2>');
      win2.document.write('<table BORDER>');
    ...
    
      win2.document.write('</table>');
      win2.document.write('<br>');
      win2.document.write ('<a href="javascript:window.print()">Print</a>');
      win2.focus();
    
    }; //DisplayPopup
    However, the link to print the content of this window (<a href="javascript:window.print()">Print</a>) doesn't work in IE (no error, no action). This code works in FF and Chrome, bringing the standard window print dialog and properly printing the popup content - exactly as desired.

    Could anybody point me to what I do wrong here and how to do this right?
    IE is the standard browser for us and even IE-specific solution could be OK...
    We have IE 7 if that matters.

    Thank you very much for your help!

  2. #2
    Use this jquery instead, some developer write this unfortunately I can 't find his website link for credit, but all credit goes to him...
    Code:
    	<script type="text/javascript">
      google.load("jquery", "1.3.2");
     
      google.setOnLoadCallback(function() {
    	
    	$(
    			function(){
     
    				// Hook up the print link.
    				$( "a#print-area" )
    					.attr( "href", "javascript:void( 0 )" )
    					.click(
    						function(){
    							// Print the DIV.
    							$( ".printable" ).print();
     
    							// Cancel click event.
    							return( false );
    						}
    						)
    				;
     
    			}
    			);
     });
    	</script>
    jquery.print.js
    Code:
    // Create a jquery plugin that prints the given element.
    jQuery.fn.print = function(){
    	// NOTE: We are trimming the jQuery collection down to the
    	// first element in the collection.
    	if (this.size() > 1){
    		this.eq( 0 ).print();
    		return;
    	} else if (!this.size()){
    		return;
    	}
     
    	// ASSERT: At this point, we know that the current jQuery
    	// collection (as defined by THIS), contains only one
    	// printable element.
     
    	// Create a random name for the print frame.
    	var strFrameName = ("printer-" + (new Date()).getTime());
     
    	// Create an iFrame with the new name.
    	var jFrame = $( "<iframe name='" + strFrameName + "'>" );
     
    	// Hide the frame (sort of) and attach to the body.
    	jFrame
    		.css( "width", "1px" )
    		.css( "height", "1px" )
    		.css( "position", "absolute" )
    		.css( "left", "-9999px" )
    		.appendTo( $( "body:first" ) )
    	;
     
    	// Get a FRAMES reference to the new frame.
    	var objFrame = window.frames[ strFrameName ];
     
    	// Get a reference to the DOM in the new frame.
    	var objDoc = objFrame.document;
     
    	// Grab all the style tags and copy to the new
    	// document so that we capture look and feel of
    	// the current document.
     
    	// Create a temp document DIV to hold the style tags.
    	// This is the only way I could find to get the style
    	// tags into IE.
    	var jStyleDiv = $( "<div>" ).append(
    		$( "style" ).clone()
    		);
     
    	// Write the HTML for the document. In this, we will
    	// write out the HTML of the current element.
    	objDoc.open();
    	objDoc.write( "<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Transitional//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd\">" );
    	objDoc.write( "<html>" );
    	objDoc.write( "<body>" );
    	objDoc.write( "<head>" );
    	objDoc.write( "<title>" );
    	objDoc.write( document.title );
    	objDoc.write( "</title>" );
    	objDoc.write( jStyleDiv.html() );
    	objDoc.write( "</head>" );
    	objDoc.write( this.html() );
    	objDoc.write( "</body>" );
    	objDoc.write( "</html>" );
    	objDoc.close();
     
    	// Print the document.
    	objFrame.focus();
    	objFrame.print();
     
    	// Have the frame remove itself in about a minute so that
    	// we don't build up too many of these frames.
    	setTimeout(
    		function(){
    			jFrame.remove();
    		},
    		(60 * 1000)
    		);
    }

  3. #3
    Join Date
    Sep 2009
    Posts
    33
    You shouldn't need jQuery for that.

    win2.document.write ('<a href="#" onclick="window.print()">Print</a>');

    Your window.open also needs the argument string to be terminated.

  4. #4
    Join Date
    Sep 2009
    Posts
    6
    win2.document.write ('<a href="#" onclick="window.print()">Print</a>');
    doesn't work either.

    I bet I had just "print()" (without "window.") working, but now it again doesn't... Mystery...

    Anybody can suggest other solution simpler than jquery (which I didn't try yet)? I think it should be simple, I should have missed some trivial thing here...

  5. #5
    Join Date
    Sep 2009
    Posts
    33
    I just tried window.print() on a link in a popup window and it works fine for me. What browser are you using that's not working?

    You could always print the window object from the opening window, but that's really essentially the same thing.

  6. #6
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by Baps View Post
    I just tried window.print() on a link in a popup window and it works fine for me. What browser are you using that's not working?
    IE 7.0.
    I wonder if there could be some security settings in play?

    Quote Originally Posted by Baps View Post
    You could always print the window object from the opening window, but that's really essentially the same thing.
    The customers require "easy" printing of the popup. Right-click menu "Print" item works, but this is not easy...

  7. #7
    Join Date
    May 2003
    Location
    Between Baltimore and DC
    Posts
    3,579
    needs

    win2.document.close();

    Eric

  8. #8
    Join Date
    Sep 2009
    Posts
    6
    Quote Originally Posted by A1ien51 View Post
    win2.document.close();
    Added. Doesn't work...
    It looks like something goes on behind the scene when the hyperlink is clicked (the hard drive is clicking for a couple of seconds, like it does when the print dialog is actually opening ), but nothing actually happens.

    Strange, the very same code (either onclick or just window.print), if placed on the main window, works perfect.
    Last edited by dp963; 09-16-2009 at 09:41 PM.

  9. #9
    Join Date
    Feb 2009
    Location
    Athnes
    Posts
    4
    deleted
    Last edited by robdraw; 09-17-2009 at 02:47 AM. Reason: mistake

  10. #10
    Join Date
    Sep 2009
    Posts
    33
    As I said, this works for me in IE7. Do you have a link we could check?

  11. #11
    Join Date
    Mar 2010
    Posts
    1

    resolved

    I had the same problem on IE8:
    Opening a new popup window. Then writing HTML-code to that popup window with document.write().

    Solutions that worked for me:

    1.From out original page:
    Code:
    document.close();
    document.print();
    2.From out of the Popup window:
    Code:
    <input type="button" value="print" onclick="eval('window.print()')"/>
    or
    <a href="javascript:eval('window.print()')"/>Print</a>
    Good Luck, JDK

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles