简体   繁体   中英

Why print function doesn't work on Firefox

Bonjour à tous

I'm trying to make a print function in javascript/jquery. My code works very well on chrome and Internet Explorer. On the other hand, it doesn't work at all on Firefox.

I do have the latest Firefox update. I tested different solutions found everywhere on the internet (like using a setTimeout, etc). But it doesn't work, it's impossible to open the print popup to make a print on Firefox.

function printDiv() {
  let str = $('#printableTable').html();
  window.frames["print_frame"].document.body.innerHTML = str;
  window.frames["print_frame"].window.focus();
  window.frames["print_frame"].window.print();
}

$('.printDiv').click(function() {
  printDiv();
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="printableTable">

  <h1 id="cnpePrint" style="text-align: center;"></h1>
  <h2 id="servicePrint" style="text-align: center;"></h2>

  <table style="border-collapse:collapse;border: 1px solid black;text-align:center;margin:auto;">
    <thead>
      <tr style="border: 1px solid black;">
        <th style="border: 1px solid black;width:100px;">Nom</th>
        <th style="border: 1px solid black;width:100px;">Prénom</th>
        <th style="border: 1px solid black;width:100px;">N° court</th>
      </tr>
    </thead>
    <tbody id="listPrint">

    </tbody>
  </table>
  <iframe class="print_frame" name="print_frame" width="0" height="0" frameborder="0" src="about:blank"></iframe>
</div>
<button class="printDiv">Print</button>

I'm a bit stumped on understanding why Firefox is blocking the expected event.

Thank you for your help

In the end, the solution was to make another script. It only works on Firefox and Explorer. But not completely well on Chrome, however, my customers don't have Chrome so it doesn't matter.

function printDiv() {

    let str = $('#printableTable').html();
    let mywindow = window.open('', 'PRINT', 'height=400,width=600');
    mywindow.document.write(str);

    mywindow.document.close();
    mywindow.focus();

    mywindow.print();
    mywindow.close();

    return true;
}

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