Open a new window to print the item

try opening a new window to include a print-only item. but it didn't work in IE8.

function printElement(elementId) {
  var printWindow = window.open('', '_blank',
    'status=0,toolbar=0,location=0,menubar=1,resizable=1,scrollbars=1'); 

  printWindow.document.write("<html><head></head><body></body></html>"); 

  var head = jQuery('head').clone();
  var printElement = jQuery('#' + elementId).clone();

  jQuery(printWindow.document).find('head').replaceWith(head);  // does not work in IE8
  var body = jQuery(printWindow.document).find('body');
  body.empty();
  body.append(printElement);  // does not work in IE8
  return false;
}

      

Thanks for the help.

+3


source to share


1 answer


How about this? If I understand what you want to do right, this should work for you. Tested in IE 8.

Working example in JSBin .

HTML:



<a href="#" id="one" class="trigger">Print One</a>
<a href="#" id="two" class="trigger">Print Two</a>

      

JavaScript:

var $trigger = $(".trigger"),
    printElement = function(id) {
        var printWindow = window.open('','_blank','status=0,toolbar=0,location=0,menubar=1,resizable=1,scrollbars=1'),
            html = $('html').clone(),
            printElement = $('#' + id).clone(),
            body;

      printWindow.document.write("<!DOCTYPE html><head></head><body></body></html>"); 

      $(printWindow.document).find('html')
                            .replaceWith(html);

      body = $(printWindow.document).find('body')
                                    .html(printElement);
    };

$trigger.on("click", function(e) {
  var id = $(this).attr("id");

  e.preventDefault();

  printElement(id);
});

      

+3


source







All Articles