简体   繁体   中英

Print HTML without opening in a new tab

Without rendering the HTML first, just print the HTML without opening a new tab, is that possible? Below is my existing code that works printing in a new tab:

  var w = window.open();
  var html = $(data).html();

  $(w.document.body).html(html);
  w.print();
  • remove window.open();
  • example

Get Print Data

var printData = document.getElementById('printID').innerHTML;

Get the HTML of whole page

var originalData = document.body.innerHTML;

Add printData to html

document.body.innerHTML =
 "<html><head><title></title></head><body>" +
 printData + "</body>";

Print Page

window.print();

Restore orignal HTML

document.body.innerHTML = originalData;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM